GNU Octave  4.0.0
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) 2015 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 #ifdef 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  restoreGeometry (settings->value("annotation/geometry").toByteArray ());
57 
58  // connect signals
59  connect (ui->button_box, SIGNAL (clicked (QAbstractButton *)),
60  this, SLOT (button_clicked (QAbstractButton *)));
61 
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 = ui->button_box->buttonRole (button);
95 
96  QSettings *settings = resource_manager::get_settings ();
97 
98  // save position
99  settings->setValue ("annotation/geometry",saveGeometry ());
100 
101  if (button_role == QDialogButtonBox::ApplyRole ||
102  button_role == QDialogButtonBox::AcceptRole)
103  {
104  get_gui_props ();
105  }
106 
107  if (button_role == QDialogButtonBox::RejectRole ||
108  button_role == QDialogButtonBox::AcceptRole)
109  close ();
110 }
111 
114 {
115  return props;
116 }
117 
118 void
120 {
121  // set props to the values of the gui
123 
124  Matrix position(1,4);
125  position(0) = ui->sb_x->value ();
126  position(1) = ui->sb_y->value ();
127  position(2) = ui->sb_width->value ();
128  position(3) = ui->sb_height->value ();
129  props.append (ovl ("textbox", position));
130 
131  props.append (ovl ("string", ui->edit_string->text ().toStdString ()));
132  props.append (ovl ("fitboxtotext", ui->cb_fit_box_to_text->isChecked() ? "on" : "off" ));
133  props.append (ovl ("units", ui->cb_units->currentText ().toStdString () ));
134  props.append (ovl ("horizontalalignment", ui->cb_horz_align->currentText ().toStdString () ));
135  props.append (ovl ("verticalalignment", ui->cb_vert_align->currentText ().toStdString () ));
136  props.append (ovl ("fontname", ui->cb_font_name->currentText ().toStdString()));
137  props.append (ovl ("fontsize", ui->sb_font_size->value ()));
138  props.append (ovl ("fontweight", ui->cb_font_bold->isChecked() ? "bold" : "normal" ));
139  props.append (ovl ("fontangle", ui->cb_font_italic->isChecked() ? "italic" : "normal" ));
140  props.append (ovl ("color", Utils::toRgb (ui->btn_color->palette ().color (QPalette::Button))));
141  props.append (ovl ("linestyle", ui->cb_line_style->currentText ().toStdString ()));
142 }
143 
144 void
146 {
147  // set the gui to the values from the props
148  octave_idx_type len = props.length ();
149 
150  for (int i=0; i<len/2; i++)
151  {
152  std::string name = props(i*2).string_value ();
153 
154  if (name == "textbox")
155  {
156  Matrix position = props(2*i +1).matrix_value ();
157  int nels = position.numel();
158  if (nels >= 2)
159  {
160  ui->sb_x->setValue (position(0));
161  ui->sb_y->setValue (position(1));
162  }
163  else
164  {
165  ui->sb_x->setValue (0);
166  ui->sb_y->setValue (0);
167  }
168  if (nels >= 4)
169  {
170  ui->sb_width->setValue (position(2));
171  ui->sb_height->setValue (position(3));
172  }
173  else
174  {
175  ui->sb_width->setValue (position(2));
176  ui->sb_height->setValue (position(3));
177  }
178  }
179  else if (name == "string")
180  {
181  // FIXME: handle if is array of strings ?
182  ui->edit_string->setText (props(2*i +1).string_value ().c_str ());
183  }
184  else if (name == "fitboxtotext")
185  {
186  ui->cb_fit_box_to_text->setChecked ( props(1*i +1).string_value () == "on" );
187  }
188  else if (name == "units")
189  {
190  ui->cb_units->setCurrentIndex ( ui->cb_units->findText(props(1*i +1).string_value ().c_str ()) );
191  }
192  else if (name == "horizontalalignment")
193  {
194  ui->cb_horz_align->setCurrentIndex ( ui->cb_horz_align->findText(props(1*i +1).string_value ().c_str ()) );
195  }
196  else if (name == "verticalalignment")
197  {
198  ui->cb_vert_align->setCurrentIndex ( ui->cb_vert_align->findText(props(1*i +1).string_value ().c_str ()) );
199  }
200  else if (name == "fontname")
201  {
202  ui->cb_vert_align->setCurrentIndex ( ui->cb_font_name->findText(props(1*i +1).string_value ().c_str ()) );
203  }
204  else if (name == "fontsize")
205  {
206  ui->sb_font_size->setValue ( props(1*i +1).float_value () );
207  }
208  else if (name == "fontweight")
209  {
210  ui->cb_font_bold->setChecked ( props(1*i +1).string_value () == "bold" );
211  }
212  else if (name == "fontangle")
213  {
214  ui->cb_font_italic->setChecked ( props(1*i +1).string_value () == "italic" );
215  }
216  else if (name == "color")
217  {
218  QColor color;
219  if (props(1*i +1).is_matrix_type ())
220  color = Utils::fromRgb (props(2*i +1).matrix_value ());
221  else
222  color.setNamedColor (props(2*i +1).string_value ().c_str ());
223 
224  if (color.isValid ())
225  ui->btn_color->setPalette (QPalette (color));
226  }
227 
228  }
229 
230  edit_string_changed (ui->edit_string->text ());
231 }
232 
233 void
235 {
236  ui->button_box->button (QDialogButtonBox::Ok)->setEnabled (str.length () > 0);
237 }
238 
239 void
241 {
242  QWidget *widg = dynamic_cast<QWidget*> (sender ());
243  if (widg)
244  {
245  QColor color = widg->palette ().color (QPalette::Button);
246 
247  color = QColorDialog::getColor(color, this);
248 
249  if (color.isValid ())
250  {
251  widg->setPalette (QPalette (color));
252 
253  QString css = QString ("background-color: %1; border: 1px solid %2;")
254  .arg (color.name ())
255  .arg ("#000000");
256 
257  widg->setStyleSheet (css);
258  widg->update ();
259  }
260  }
261 }
262 
void edit_string_changed(const QString &str)
void setupUi(QDialog *annotation_dialog)
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:275
octave_idx_type length(void) const
Definition: oct-obj.h:89
octave_value_list & append(const octave_value &val)
Definition: oct-obj.cc:85
QDoubleSpinBox * sb_width
void button_clicked(QAbstractButton *button)
octave_value_list props
QDialogButtonBox * button_box
QColor fromRgb(const Matrix &rgb)
Matrix toRgb(const QColor &c)
Definition: dMatrix.h:35
static QSettings * get_settings(void)
double arg(double x)
Definition: lo-mappers.h:37
octave_value_list ovl(const octave_value &a0)
Definition: oct-obj.h:178
QFontComboBox * cb_font_name
octave_value_list get_properties() const
Ui::annotation_dialog * ui
QDoubleSpinBox * sb_height
QPushButton * btn_background_color
annotation_dialog(QWidget *parent, const octave_value_list &pr)