GNU Octave 7.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
annotation-dialog.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2016-2022 The Octave Project Developers
4//
5// See the file COPYRIGHT.md in the top-level directory of this
6// distribution or <https://octave.org/copyright/>.
7//
8// This file is part of Octave.
9//
10// Octave is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// Octave is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Octave; see the file COPYING. If not, see
22// <https://www.gnu.org/licenses/>.
23//
24////////////////////////////////////////////////////////////////////////
25
26#if defined (HAVE_CONFIG_H)
27# include "config.h"
28#endif
29
30#include <QColorDialog>
31#include <QPushButton>
32#include <QPalette>
33
34#include "gui-settings.h"
35#include "gui-preferences-gp.h"
36#include "octave-qobject.h"
37
38#include "QtHandlesUtils.h"
39#include "annotation-dialog.h"
40#include "ui-annotation-dialog.h"
41
42using namespace octave;
43
45 QWidget *p, const octave_value_list& pr):
46 QDialog (p), m_octave_qobj (oct_qobj), ui (new Ui::annotation_dialog)
47{
48 props = pr;
49
50 init ();
51}
52
53void
55{
56 ui->setupUi (this);
57
59
61
62 // restore last geometry
64 restoreGeometry (settings->value (gp_annotation_geometry).toByteArray ());
65
66 // connect signals
67 connect (ui->button_box, &QDialogButtonBox::clicked,
69
70 connect (ui->edit_string, &QLineEdit::textChanged,
72
73 connect (ui->btn_color, &QPushButton::clicked,
75
76 connect (ui->btn_background_color, &QPushButton::clicked,
78
79 connect (ui->btn_edge_color, &QPushButton::clicked,
81
82 // set gui element to default values
83 ui->cb_fit_box_to_text->setChecked (true);
84 ui->cb_horz_align->setCurrentIndex (ui->cb_horz_align->findText ("left"));
85 ui->cb_vert_align->setCurrentIndex (ui->cb_vert_align->findText ("middle"));
86
87 // set gui elements to any values from input properties
89}
90
92{
93 delete ui;
94}
95
96// internal slots
97
98void
99annotation_dialog::button_clicked (QAbstractButton *button)
100{
101 QDialogButtonBox::ButtonRole button_role
102 = ui->button_box->buttonRole (button);
103
105
107
108 // save position
109 if (settings)
110 settings->setValue (gp_annotation_geometry.key, saveGeometry ());
111
112 if (button_role == QDialogButtonBox::ApplyRole
113 || button_role == QDialogButtonBox::AcceptRole)
114 {
115 get_gui_props ();
116 }
117
118 if (button_role == QDialogButtonBox::RejectRole
119 || button_role == QDialogButtonBox::AcceptRole)
120 close ();
121}
122
125{
126 return props;
127}
128
129void
131{
132 // set props to the values of the gui
134
135 Matrix position(1, 4);
136 position(0) = ui->sb_x->value ();
137 position(1) = ui->sb_y->value ();
138 position(2) = ui->sb_width->value ();
139 position(3) = ui->sb_height->value ();
140 props.append (ovl ("textbox", position));
141
142 props.append (ovl ("string", ui->edit_string->text ().toStdString ()));
143 props.append (ovl ("fitboxtotext",
144 ui->cb_fit_box_to_text->isChecked () ? "on" : "off"));
145
146 // FIXME: only "normalized" units is selectable, change the code below
147 // once more units are added in the UI.
148 std::string tmpstr;
149 props.append (ovl ("units", "normalized"));
150
151 tmpstr = (ui->cb_horz_align->currentIndex () == 0 ? "left" :
152 (ui->cb_horz_align->currentIndex () == 1 ? "center" : "right"));
153 props.append (ovl ("horizontalalignment", tmpstr));
154
155 tmpstr = (ui->cb_vert_align->currentIndex () == 0 ? "top" :
156 (ui->cb_horz_align->currentIndex () == 1 ? "middle" : "bottom"));
157 props.append (ovl ("verticalalignment", tmpstr));
158
159 tmpstr = ui->cb_font_name->currentText ().toStdString ();
160 props.append (ovl ("fontname", tmpstr));
161
162 props.append (ovl ("fontsize", ui->sb_font_size->value ()));
163 props.append (ovl ("fontweight",
164 ui->cb_font_bold->isChecked () ? "bold" : "normal"));
165 props.append (ovl ("fontangle",
166 ui->cb_font_italic->isChecked () ? "italic" : "normal"));
167 props.append (ovl ("color", Utils::toRgb (ui->btn_color->palette ().
168 color (QPalette::Button))));
169
170 // FIXME: only "none" linestyle is selectable, change the code bellow
171 // once more linestyles are added in the UI.
172 props.append (ovl ("linestyle", "none"));
173}
174
175void
177{
178 // set the gui to the values from the props
180
181 for (int i=0; i<len/2; i++)
182 {
183 std::string name = props(i*2).string_value ();
184
185 if (name == "textbox")
186 {
187 Matrix position = props(2*i +1).matrix_value ();
188 int nels = position.numel ();
189 if (nels >= 2)
190 {
191 ui->sb_x->setValue (position(0));
192 ui->sb_y->setValue (position(1));
193 }
194 else
195 {
196 ui->sb_x->setValue (0);
197 ui->sb_y->setValue (0);
198 }
199 if (nels >= 4)
200 {
201 ui->sb_width->setValue (position(2));
202 ui->sb_height->setValue (position(3));
203 }
204 // FIXME: Should there be an else branch here?
205 // In annotation.m "textbox" is forced to have a 4-elem vector.
206 }
207 else if (name == "string")
208 {
209 // FIXME: handle if is array of strings ?
210 ui->edit_string->setText (props(2*i +1).string_value ().c_str ());
211 }
212 else if (name == "fitboxtotext")
213 {
214 ui->cb_fit_box_to_text->setChecked (props(1*i +1).string_value () == "on");
215 }
216 else if (name == "units")
217 {
218 ui->cb_units->setCurrentIndex
219 (ui->cb_units->findText (props(1*i +1).string_value ().c_str ()));
220 }
221 else if (name == "horizontalalignment")
222 {
223 ui->cb_horz_align->setCurrentIndex
224 (ui->cb_horz_align->findText (props(1*i +1).string_value ().c_str ()));
225 }
226 else if (name == "verticalalignment")
227 {
228 ui->cb_vert_align->setCurrentIndex
229 (ui->cb_vert_align->findText (props(1*i +1).string_value ().c_str ()));
230 }
231 else if (name == "fontname")
232 {
233 ui->cb_vert_align->setCurrentIndex
234 (ui->cb_font_name->findText (props(1*i +1).string_value ().c_str ()));
235 }
236 else if (name == "fontsize")
237 {
238 ui->sb_font_size->setValue (props(1*i +1).float_value ());
239 }
240 else if (name == "fontweight")
241 {
242 ui->cb_font_bold->setChecked (props(1*i +1).string_value () == "bold");
243 }
244 else if (name == "fontangle")
245 {
246 ui->cb_font_italic->setChecked (props(1*i +1).string_value () == "italic");
247 }
248 else if (name == "color")
249 {
250 QColor color;
251 if (props(1*i +1).is_matrix_type ())
252 color = Utils::fromRgb (props(2*i +1).matrix_value ());
253 else
254 color.setNamedColor (props(2*i +1).string_value ().c_str ());
255
256 if (color.isValid ())
257 ui->btn_color->setPalette (QPalette (color));
258 }
259
260 }
261
262 edit_string_changed (ui->edit_string->text ());
263}
264
265void
267{
268 ui->button_box->button (QDialogButtonBox::Ok)->setEnabled (str.length () > 0);
269}
270
271void
273{
274 QWidget *widg = dynamic_cast<QWidget *> (sender ());
275 if (widg)
276 {
277 QColor color = widg->palette ().color (QPalette::Button);
278
279 color = QColorDialog::getColor (color, this);
280
281 if (color.isValid ())
282 {
283 widg->setPalette (QPalette (color));
284
285 QString css = QString ("background-color: %1; border: 1px solid %2;")
286 .arg (color.name ())
287 .arg ("#000000");
288
289 widg->setStyleSheet (css);
290 widg->update ();
291 }
292 }
293}
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:411
Definition: dMatrix.h:42
void button_clicked(QAbstractButton *button)
void edit_string_changed(const QString &str)
octave_value_list get_properties() const
octave::base_qobject & m_octave_qobj
annotation_dialog(octave::base_qobject &oct_qobj, QWidget *parent, const octave_value_list &pr)
octave_value_list props
Ui::annotation_dialog * ui
Base class for Octave interfaces that use Qt.
resource_manager & get_resource_manager(void)
gui_settings * get_settings(void) const
octave_value_list & append(const octave_value &val)
Definition: ovl.cc:98
octave_idx_type length(void) const
Definition: ovl.h:113
QString name
const gui_pref gp_annotation_geometry("annotation/geometry", QVariant())
QColor fromRgb(const Matrix &rgb)
Matrix toRgb(const QColor &c)
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition: ovl.h:211
const QString key
F77_RET_T len
Definition: xerbla.cc:61