GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
annotation-dialog.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2016-2017 John Donoghue
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include "annotation-dialog.h"
28 #include "ui-annotation-dialog.h"
29 
30 #include "QtHandlesUtils.h"
31 
32 #include "resource-manager.h"
33 
34 #include <QColorDialog>
35 #include <QPushButton>
36 #include <QPalette>
37 
38 using namespace QtHandles;
39 
41  QDialog (p), ui (new Ui::annotation_dialog)
42 {
43  props = pr;
44 
45  init ();
46 }
47 
48 void
50 {
51  ui->setupUi (this);
52 
53  QSettings *settings = resource_manager::get_settings ();
54 
55  // restore last geometry
56  if (settings)
57  restoreGeometry (settings->value("annotation/geometry").toByteArray ());
58 
59  // connect signals
60  connect (ui->button_box, SIGNAL (clicked (QAbstractButton *)),
61  this, SLOT (button_clicked (QAbstractButton *)));
62 
63  connect (ui->edit_string, SIGNAL (textChanged (const QString&)),
64  this, SLOT (edit_string_changed (const QString&)));
65 
66  connect (ui->btn_color, SIGNAL (clicked ()),
67  this, SLOT (prompt_for_color ()));
68 
69  connect (ui->btn_background_color, SIGNAL (clicked ()),
70  this, SLOT (prompt_for_color ()));
71 
72  connect (ui->btn_edge_color, SIGNAL (clicked ()),
73  this, SLOT (prompt_for_color ()));
74 
75  // set gui element to default values
76  ui->cb_fit_box_to_text->setChecked (true);
77  ui->cb_horz_align->setCurrentIndex (ui->cb_horz_align->findText("left"));
78  ui->cb_vert_align->setCurrentIndex (ui->cb_vert_align->findText("middle"));
79 
80  // set gui elements to any values from input properties
81  set_gui_props ();
82 }
83 
85 {
86  delete ui;
87 }
88 
89 // internal slots
90 
91 void
92 annotation_dialog::button_clicked (QAbstractButton *button)
93 {
94  QDialogButtonBox::ButtonRole button_role
95  = ui->button_box->buttonRole (button);
96 
97  QSettings *settings = resource_manager::get_settings ();
98 
99  // save position
100  if (settings)
101  settings->setValue ("annotation/geometry",saveGeometry ());
102 
103  if (button_role == QDialogButtonBox::ApplyRole
104  || button_role == QDialogButtonBox::AcceptRole)
105  {
106  get_gui_props ();
107  }
108 
109  if (button_role == QDialogButtonBox::RejectRole
110  || button_role == QDialogButtonBox::AcceptRole)
111  close ();
112 }
113 
116 {
117  return props;
118 }
119 
120 void
122 {
123  // set props to the values of the gui
125 
126  Matrix position(1,4);
127  position(0) = ui->sb_x->value ();
128  position(1) = ui->sb_y->value ();
129  position(2) = ui->sb_width->value ();
130  position(3) = ui->sb_height->value ();
131  props.append (ovl ("textbox", position));
132 
133  props.append (ovl ("string", ui->edit_string->text ().toStdString ()));
134  props.append (ovl ("fitboxtotext",
135  ui->cb_fit_box_to_text->isChecked () ? "on" : "off"));
136 
137  // FIXME: only "normalized" units is selectable, change the code below
138  // once more units are added in the UI.
139  std::string tmpstr;
140  props.append (ovl ("units", "normalized"));
141 
142  tmpstr = (ui->cb_horz_align->currentIndex () == 0 ? "left" :
143  (ui->cb_horz_align->currentIndex () == 1 ? "center" : "right"));
144  props.append (ovl ("horizontalalignment", tmpstr));
145 
146  tmpstr = (ui->cb_vert_align->currentIndex () == 0 ? "top" :
147  (ui->cb_horz_align->currentIndex () == 1 ? "middle" : "bottom"));
148  props.append (ovl ("verticalalignment", tmpstr));
149 
150  tmpstr = ui->cb_font_name->currentText ().toStdString();
151  props.append (ovl ("fontname", tmpstr));
152 
153  props.append (ovl ("fontsize", ui->sb_font_size->value ()));
154  props.append (ovl ("fontweight",
155  ui->cb_font_bold->isChecked () ? "bold" : "normal"));
156  props.append (ovl ("fontangle",
157  ui->cb_font_italic->isChecked () ? "italic" : "normal"));
158  props.append (ovl ("color", Utils::toRgb (ui->btn_color->palette ().
159  color (QPalette::Button))));
160 
161  // FIXME: only "none" linestyle is selectable, change the code bellow
162  // once more linestyles are added in the UI.
163  props.append (ovl ("linestyle", "none"));
164 }
165 
166 void
168 {
169  // set the gui to the values from the props
170  octave_idx_type len = props.length ();
171 
172  for (int i=0; i<len/2; i++)
173  {
174  std::string name = props(i*2).string_value ();
175 
176  if (name == "textbox")
177  {
178  Matrix position = props(2*i +1).matrix_value ();
179  int nels = position.numel();
180  if (nels >= 2)
181  {
182  ui->sb_x->setValue (position(0));
183  ui->sb_y->setValue (position(1));
184  }
185  else
186  {
187  ui->sb_x->setValue (0);
188  ui->sb_y->setValue (0);
189  }
190  if (nels >= 4)
191  {
192  ui->sb_width->setValue (position(2));
193  ui->sb_height->setValue (position(3));
194  }
195  else
196  {
197  ui->sb_width->setValue (position(2));
198  ui->sb_height->setValue (position(3));
199  }
200  }
201  else if (name == "string")
202  {
203  // FIXME: handle if is array of strings ?
204  ui->edit_string->setText (props(2*i +1).string_value ().c_str ());
205  }
206  else if (name == "fitboxtotext")
207  {
208  ui->cb_fit_box_to_text->setChecked (props(1*i +1).string_value () == "on");
209  }
210  else if (name == "units")
211  {
212  ui->cb_units->setCurrentIndex (ui->cb_units->findText (props(
213  1*i +1).string_value ().c_str ()));
214  }
215  else if (name == "horizontalalignment")
216  {
217  ui->cb_horz_align->setCurrentIndex (ui->cb_horz_align->findText (props(
218  1*i +1).string_value ().c_str ()));
219  }
220  else if (name == "verticalalignment")
221  {
222  ui->cb_vert_align->setCurrentIndex (ui->cb_vert_align->findText (props(
223  1*i +1).string_value ().c_str ()));
224  }
225  else if (name == "fontname")
226  {
227  ui->cb_vert_align->setCurrentIndex (ui->cb_font_name->findText (props(
228  1*i +1).string_value ().c_str ()));
229  }
230  else if (name == "fontsize")
231  {
232  ui->sb_font_size->setValue (props(1*i +1).float_value ());
233  }
234  else if (name == "fontweight")
235  {
236  ui->cb_font_bold->setChecked (props(1*i +1).string_value () == "bold");
237  }
238  else if (name == "fontangle")
239  {
240  ui->cb_font_italic->setChecked (props(1*i +1).string_value () == "italic");
241  }
242  else if (name == "color")
243  {
244  QColor color;
245  if (props(1*i +1).is_matrix_type ())
246  color = Utils::fromRgb (props(2*i +1).matrix_value ());
247  else
248  color.setNamedColor (props(2*i +1).string_value ().c_str ());
249 
250  if (color.isValid ())
251  ui->btn_color->setPalette (QPalette (color));
252  }
253 
254  }
255 
256  edit_string_changed (ui->edit_string->text ());
257 }
258 
259 void
261 {
262  ui->button_box->button (QDialogButtonBox::Ok)->setEnabled (str.length () > 0);
263 }
264 
265 void
267 {
268  QWidget *widg = dynamic_cast<QWidget*> (sender ());
269  if (widg)
270  {
271  QColor color = widg->palette ().color (QPalette::Button);
272 
273  color = QColorDialog::getColor(color, this);
274 
275  if (color.isValid ())
276  {
277  widg->setPalette (QPalette (color));
278 
279  QString css = QString ("background-color: %1; border: 1px solid %2;")
280  .arg (color.name ())
281  .arg ("#000000");
282 
283  widg->setStyleSheet (css);
284  widg->update ();
285  }
286  }
287 }
void edit_string_changed(const QString &str)
OCTAVE_EXPORT octave_value_list isa nd deftypefn *return ovl(args(0).is_integer_type())
void setupUi(QDialog *annotation_dialog)
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:363
octave_idx_type length(void) const
Definition: ovl.h:96
octave_value_list & append(const octave_value &val)
Definition: ovl.cc:85
QDoubleSpinBox * sb_width
void button_clicked(QAbstractButton *button)
octave_value_list props
octave_value arg
Definition: pr-output.cc:3440
QDialogButtonBox * button_box
QColor fromRgb(const Matrix &rgb)
OCTAVE_EXPORT octave_value_list any number nd example oindent prints the prompt xample Pick a any number!nd example oindent and waits for the user to enter a value The string entered by the user is evaluated as an so it may be a literal a variable name
Definition: input.cc:871
Matrix toRgb(const QColor &c)
std::string str
Definition: hash.cc:118
Definition: dMatrix.h:37
static QSettings * get_settings(void)
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
QFontComboBox * cb_font_name
p
Definition: lu.cc:138
octave_value_list get_properties() const
Ui::annotation_dialog * ui
QDoubleSpinBox * sb_height
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:854
QPushButton * btn_background_color
annotation_dialog(QWidget *parent, const octave_value_list &pr)