GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
Panel.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2011-2018 Michael Goffioul
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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include <QEvent>
28 #include <QFrame>
29 #include <QLabel>
30 #include <QMouseEvent>
31 #include <QTimer>
32 
33 #include "Canvas.h"
34 #include "Container.h"
35 #include "ContextMenu.h"
36 #include "Panel.h"
37 #include "QtHandlesUtils.h"
38 
39 namespace QtHandles
40 {
41 
42  static int
44  {
45  if (pp.bordertype_is ("none"))
46  return QFrame::NoFrame;
47  else if (pp.bordertype_is ("etchedin"))
48  return (QFrame::Box | QFrame::Sunken);
49  else if (pp.bordertype_is ("etchedout"))
50  return (QFrame::Box | QFrame::Raised);
51  else if (pp.bordertype_is ("beveledin"))
52  return (QFrame::Panel | QFrame::Sunken);
53  else if (pp.bordertype_is ("beveledout"))
54  return (QFrame::Panel | QFrame::Raised);
55  else
56  return (QFrame::Panel | QFrame::Plain);
57  }
58 
59  static void
60  setupPalette (const uipanel::properties& pp, QPalette& p)
61  {
62  p.setColor (QPalette::Window,
63  Utils::fromRgb (pp.get_backgroundcolor_rgb ()));
64  p.setColor (QPalette::WindowText,
65  Utils::fromRgb (pp.get_foregroundcolor_rgb ()));
66  p.setColor (QPalette::Light,
67  Utils::fromRgb (pp.get_highlightcolor_rgb ()));
68  p.setColor (QPalette::Dark,
69  Utils::fromRgb (pp.get_shadowcolor_rgb ()));
70  }
71 
72  static int
74  {
75  int bw = 0;
76 
77  if (! pp.bordertype_is ("none"))
78  {
79  bw = octave::math::round (pp.get_borderwidth ());
80  if (pp.bordertype_is ("etchedin") || pp.bordertype_is ("etchedout"))
81  bw *= 2;
82  }
83 
84  return bw;
85  }
86 
87  Panel*
89  {
90  Object *parent = Object::parentObject (go);
91 
92  if (parent)
93  {
94  Container *container = parent->innerContainer ();
95 
96  if (container)
97  return new Panel (go, new QFrame (container));
98  }
99 
100  return nullptr;
101  }
102 
103  Panel::Panel (const graphics_object& go, QFrame *frame)
104  : Object (go, frame), m_container (nullptr), m_title (nullptr),
105  m_blockUpdates (false)
106  {
107  uipanel::properties& pp = properties<uipanel> ();
108 
109  frame->setObjectName ("UIPanel");
110  frame->setAutoFillBackground (true);
111  Matrix bb = pp.get_boundingbox (false);
112  frame->setGeometry (octave::math::round (bb(0)), octave::math::round (bb(1)),
113  octave::math::round (bb(2)), octave::math::round (bb(3)));
114  frame->setFrameStyle (frameStyleFromProperties (pp));
115  frame->setLineWidth (octave::math::round (pp.get_borderwidth ()));
116  QPalette pal = frame->palette ();
117  setupPalette (pp, pal);
118  frame->setPalette (pal);
119 
120  m_container = new Container (frame);
122 
123  if (frame->hasMouseTracking ())
124  {
125  foreach (QWidget *w, frame->findChildren<QWidget*> ())
126  w->setMouseTracking (true);
127  }
128 
129  QString title = Utils::fromStdString (pp.get_title ());
130  if (! title.isEmpty ())
131  {
132  m_title = new QLabel (title, frame);
133  m_title->setAutoFillBackground (true);
134  m_title->setContentsMargins (4, 0, 4, 0);
135  m_title->setPalette (pal);
136  m_title->setFont (Utils::computeFont<uipanel> (pp, bb(3)));
137  }
138 
139  frame->installEventFilter (this);
140  m_container->installEventFilter (this);
141 
142  if (pp.is_visible ())
143  QTimer::singleShot (0, frame, SLOT (show (void)));
144  else
145  frame->hide ();
146  }
147 
149  { }
150 
151  bool
152  Panel::eventFilter (QObject *watched, QEvent *xevent)
153  {
154  if (! m_blockUpdates)
155  {
156  if (watched == qObject ())
157  {
158  switch (xevent->type ())
159  {
160  case QEvent::Resize:
161  {
163  graphics_object go = object ();
164 
165  if (go.valid_object ())
166  {
167  if (m_title)
168  {
169  const uipanel::properties& pp =
170  Utils::properties<uipanel> (go);
171 
172  if (pp.fontunits_is ("normalized"))
173  {
174  QFrame *frame = qWidget<QFrame> ();
175 
177  (pp, frame->height ()));
178  m_title->resize (m_title->sizeHint ());
179  }
180  }
181  updateLayout ();
182  }
183  }
184  break;
185 
186  case QEvent::MouseButtonPress:
187  {
188  QMouseEvent *m = dynamic_cast<QMouseEvent *> (xevent);
189 
190  if (m->button () == Qt::RightButton)
191  {
193 
194  graphics_object go = object ();
195 
196  if (go.valid_object ())
198  m->globalPos ());
199  }
200  }
201  break;
202 
203  default:
204  break;
205  }
206  }
207  else if (watched == m_container)
208  {
209  switch (xevent->type ())
210  {
211  case QEvent::Resize:
212  if (qWidget<QWidget> ()->isVisible ())
213  {
215 
216  graphics_object go = object ();
217 
218  if (go.valid_object ())
220  }
221  break;
222 
223  default:
224  break;
225  }
226  }
227  }
228 
229  return false;
230  }
231 
232  void
233  Panel::update (int pId)
234  {
235  uipanel::properties& pp = properties<uipanel> ();
236  QFrame *frame = qWidget<QFrame> ();
237 
238  m_blockUpdates = true;
239 
240  switch (pId)
241  {
242  case uipanel::properties::ID_POSITION:
243  {
244  Matrix bb = pp.get_boundingbox (false);
245 
246  frame->setGeometry (octave::math::round (bb(0)), octave::math::round (bb(1)),
247  octave::math::round (bb(2)), octave::math::round (bb(3)));
248  updateLayout ();
249  }
250  break;
251 
252  case uipanel::properties::ID_BORDERWIDTH:
253  frame->setLineWidth (octave::math::round (pp.get_borderwidth ()));
254  updateLayout ();
255  break;
256 
257  case uipanel::properties::ID_BACKGROUNDCOLOR:
258  case uipanel::properties::ID_FOREGROUNDCOLOR:
259  case uipanel::properties::ID_HIGHLIGHTCOLOR:
260  case uipanel::properties::ID_SHADOWCOLOR:
261  {
262  QPalette pal = frame->palette ();
263 
264  setupPalette (pp, pal);
265  frame->setPalette (pal);
266  if (m_title)
267  m_title->setPalette (pal);
268  }
269  break;
270 
271  case uipanel::properties::ID_TITLE:
272  {
273  QString title = Utils::fromStdString (pp.get_title ());
274 
275  if (title.isEmpty ())
276  {
277  if (m_title)
278  delete m_title;
279  m_title = nullptr;
280  }
281  else
282  {
283  if (! m_title)
284  {
285  QPalette pal = frame->palette ();
286 
287  m_title = new QLabel (title, frame);
288  m_title->setAutoFillBackground (true);
289  m_title->setContentsMargins (4, 0, 4, 0);
290  m_title->setPalette (pal);
291  m_title->setFont (Utils::computeFont<uipanel> (pp));
292  m_title->show ();
293  }
294  else
295  {
296  m_title->setText (title);
297  m_title->resize (m_title->sizeHint ());
298  }
299  }
300  updateLayout ();
301  }
302  break;
303 
304  case uipanel::properties::ID_TITLEPOSITION:
305  updateLayout ();
306  break;
307 
308  case uipanel::properties::ID_BORDERTYPE:
309  frame->setFrameStyle (frameStyleFromProperties (pp));
310  updateLayout ();
311  break;
312 
313  case uipanel::properties::ID_FONTNAME:
314  case uipanel::properties::ID_FONTSIZE:
315  case uipanel::properties::ID_FONTWEIGHT:
316  case uipanel::properties::ID_FONTANGLE:
317  if (m_title)
318  {
319  m_title->setFont (Utils::computeFont<uipanel> (pp));
320  m_title->resize (m_title->sizeHint ());
321  updateLayout ();
322  }
323  break;
324 
325  case uipanel::properties::ID_VISIBLE:
326  frame->setVisible (pp.is_visible ());
327  updateLayout ();
328  break;
329 
330  default:
331  break;
332  }
333 
334  m_blockUpdates = false;
335  }
336 
337  void
339  {
340  Canvas *canvas = m_container->canvas (m_handle);
341 
342  if (canvas)
343  canvas->redraw ();
344  }
345 
346  void
348  {
349  uipanel::properties& pp = properties<uipanel> ();
350  QFrame *frame = qWidget<QFrame> ();
351 
352  Matrix bb = pp.get_boundingbox (true);
353  int bw = borderWidthFromProperties (pp);
354 
355  frame->setFrameRect (QRect (octave::math::round (bb(0)) - bw,
356  octave::math::round (bb(1)) - bw,
357  octave::math::round (bb(2)) + 2*bw, octave::math::round (bb(3)) + 2*bw));
358  m_container->setGeometry (octave::math::round (bb(0)),
359  octave::math::round (bb(1)),
360  octave::math::round (bb(2)), octave::math::round (bb(3)));
361 
362  if (m_blockUpdates)
363  pp.update_boundingbox ();
364 
365  if (m_title)
366  {
367  QSize sz = m_title->sizeHint ();
368  int offset = 5;
369 
370  if (pp.titleposition_is ("lefttop"))
371  m_title->move (bw+offset, 0);
372  else if (pp.titleposition_is ("righttop"))
373  m_title->move (frame->width () - bw - offset - sz.width (), 0);
374  else if (pp.titleposition_is ("leftbottom"))
375  m_title->move (bw+offset, frame->height () - sz.height ());
376  else if (pp.titleposition_is ("rightbottom"))
377  m_title->move (frame->width () - bw - offset - sz.width (),
378  frame->height () - sz.height ());
379  else if (pp.titleposition_is ("centertop"))
380  m_title->move (frame->width () / 2 - sz.width () / 2, 0);
381  else if (pp.titleposition_is ("centerbottom"))
382  m_title->move (frame->width () / 2 - sz.width () / 2,
383  frame->height () - sz.height ());
384  }
385  }
386 
387 };
static void setupPalette(const uibuttongroup::properties &pp, QPalette &p)
Definition: ButtonGroup.cc:68
void updateLayout(void)
Definition: Panel.cc:347
void update(int pId)
Definition: Panel.cc:233
bool eventFilter(QObject *watched, QEvent *event)
Definition: Panel.cc:152
static int frameStyleFromProperties(const uibuttongroup::properties &pp)
Definition: ButtonGroup.cc:51
static Panel * create(const graphics_object &go)
Definition: Panel.cc:88
QString fromStdString(const std::string &s)
Matrix get_boundingbox(bool internal=false, const Matrix &parent_pix_size=Matrix()) const
Definition: graphics.cc:9763
static Object * parentObject(const graphics_object &go)
Definition: Object.cc:165
void redraw(void)
Definition: Panel.cc:338
virtual Container * innerContainer(void)=0
Container * m_container
Definition: Panel.h:56
static int borderWidthFromProperties(const uibuttongroup::properties &pp)
Definition: ButtonGroup.cc:81
void redraw(bool sync=false)
Definition: Canvas.cc:53
virtual void update_boundingbox(void)
Definition: graphics.cc:3285
static void executeAt(const base_properties &props, const QPoint &pt)
Definition: ContextMenu.cc:113
QColor fromRgb(const Matrix &rgb)
bool valid_object(void) const
Definition: graphics.in.h:2806
~Panel(void)
Definition: Panel.cc:148
std::complex< double > w(std::complex< double > z, double relerr=0)
graphics_handle m_handle
Definition: Object.h:115
is false
Definition: cellfun.cc:400
base_properties & get_properties(void)
Definition: graphics.in.h:2788
template QFont computeFont< uipanel >(const uipanel::properties &props, int height)
Definition: dMatrix.h:36
sz
Definition: data.cc:5264
QLabel * m_title
Definition: Panel.h:57
virtual QObject * qObject(void)
Definition: Object.h:71
graphics_object object(void) const
Definition: Object.cc:72
bool m_blockUpdates
Definition: Panel.h:58
Panel(const graphics_object &go, QFrame *frame)
Definition: Panel.cc:103
octave::unwind_protect frame
Definition: graphics.cc:12190
p
Definition: lu.cc:138
double round(double x)
Definition: lo-mappers.h:145
Canvas * canvas(const graphics_handle &handle, bool create=true)
Definition: Container.cc:50