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
Panel.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2011-2017 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 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 <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,
64  p.setColor (QPalette::WindowText,
66  p.setColor (QPalette::Light,
68  p.setColor (QPalette::Dark,
70  }
71 
72  static int
74  {
75  int bw = 0;
76 
77  if (! pp.bordertype_is ("none"))
78  {
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 0;
101  }
102 
103  Panel::Panel (const graphics_object& go, QFrame* frame)
104  : Object (go, frame), m_container (0), m_title (0), m_blockUpdates (false)
105  {
106  uipanel::properties& pp = properties<uipanel> ();
107 
108  frame->setObjectName ("UIPanel");
109  frame->setAutoFillBackground (true);
110  Matrix bb = pp.get_boundingbox (false);
111  frame->setGeometry (octave::math::round (bb(0)), octave::math::round (bb(1)),
112  octave::math::round (bb(2)), octave::math::round (bb(3)));
113  frame->setFrameStyle (frameStyleFromProperties (pp));
114  frame->setLineWidth (octave::math::round (pp.get_borderwidth ()));
115  QPalette pal = frame->palette ();
116  setupPalette (pp, pal);
117  frame->setPalette (pal);
118 
119  m_container = new Container (frame);
121 
122  if (frame->hasMouseTracking ())
123  {
124  foreach (QWidget* w, frame->findChildren<QWidget*> ())
125  { w->setMouseTracking (true); }
126  }
127 
128  QString title = Utils::fromStdString (pp.get_title ());
129  if (! title.isEmpty ())
130  {
131  m_title = new QLabel (title, frame);
132  m_title->setAutoFillBackground (true);
133  m_title->setContentsMargins (4, 0, 4, 0);
134  m_title->setPalette (pal);
135  m_title->setFont (Utils::computeFont<uipanel> (pp, bb(3)));
136  }
137 
138  frame->installEventFilter (this);
139  m_container->installEventFilter (this);
140 
141  if (pp.is_visible ())
142  QTimer::singleShot (0, frame, SLOT (show (void)));
143  else
144  frame->hide ();
145  }
146 
148  { }
149 
150  bool
151  Panel::eventFilter (QObject* watched, QEvent* xevent)
152  {
153  if (! m_blockUpdates)
154  {
155  if (watched == qObject ())
156  {
157  switch (xevent->type ())
158  {
159  case QEvent::Resize:
160  {
162  graphics_object go = object ();
163 
164  if (go.valid_object ())
165  {
166  if (m_title)
167  {
168  const uipanel::properties& pp =
169  Utils::properties<uipanel> (go);
170 
171  if (pp.fontunits_is ("normalized"))
172  {
173  QFrame* frame = qWidget<QFrame> ();
174 
176  (pp, frame->height ()));
177  m_title->resize (m_title->sizeHint ());
178  }
179  }
180  updateLayout ();
181  }
182  }
183  break;
184 
185  case QEvent::MouseButtonPress:
186  {
187  QMouseEvent* m = dynamic_cast<QMouseEvent*> (xevent);
188 
189  if (m->button () == Qt::RightButton)
190  {
192 
193  graphics_object go = object ();
194 
195  if (go.valid_object ())
197  m->globalPos ());
198  }
199  }
200  break;
201 
202  default:
203  break;
204  }
205  }
206  else if (watched == m_container)
207  {
208  switch (xevent->type ())
209  {
210  case QEvent::Resize:
211  if (qWidget<QWidget> ()->isVisible ())
212  {
214 
215  graphics_object go = object ();
216 
217  if (go.valid_object ())
219  }
220  break;
221 
222  default:
223  break;
224  }
225  }
226  }
227 
228  return false;
229  }
230 
231  void
232  Panel::update (int pId)
233  {
234  uipanel::properties& pp = properties<uipanel> ();
235  QFrame* frame = qWidget<QFrame> ();
236 
237  m_blockUpdates = true;
238 
239  switch (pId)
240  {
242  {
243  Matrix bb = pp.get_boundingbox (false);
244 
245  frame->setGeometry (octave::math::round (bb(0)), octave::math::round (bb(1)),
246  octave::math::round (bb(2)), octave::math::round (bb(3)));
247  updateLayout ();
248  }
249  break;
250 
252  frame->setLineWidth (octave::math::round (pp.get_borderwidth ()));
253  updateLayout ();
254  break;
255 
260  {
261  QPalette pal = frame->palette ();
262 
263  setupPalette (pp, pal);
264  frame->setPalette (pal);
265  if (m_title)
266  m_title->setPalette (pal);
267  }
268  break;
269 
271  {
272  QString title = Utils::fromStdString (pp.get_title ());
273 
274  if (title.isEmpty ())
275  {
276  if (m_title)
277  delete m_title;
278  m_title = 0;
279  }
280  else
281  {
282  if (! m_title)
283  {
284  QPalette pal = frame->palette ();
285 
286  m_title = new QLabel (title, frame);
287  m_title->setAutoFillBackground (true);
288  m_title->setContentsMargins (4, 0, 4, 0);
289  m_title->setPalette (pal);
290  m_title->setFont (Utils::computeFont<uipanel> (pp));
291  m_title->show ();
292  }
293  else
294  {
295  m_title->setText (title);
296  m_title->resize (m_title->sizeHint ());
297  }
298  }
299  updateLayout ();
300  }
301  break;
302 
304  updateLayout ();
305  break;
306 
308  frame->setFrameStyle (frameStyleFromProperties (pp));
309  updateLayout ();
310  break;
311 
316  if (m_title)
317  {
318  m_title->setFont (Utils::computeFont<uipanel> (pp));
319  m_title->resize (m_title->sizeHint ());
320  updateLayout ();
321  }
322  break;
323 
325  frame->setVisible (pp.is_visible ());
326  updateLayout ();
327  break;
328 
329  default:
330  break;
331  }
332 
333  m_blockUpdates = false;
334  }
335 
336  void
338  {
339  Canvas* canvas = m_container->canvas (m_handle);
340 
341  if (canvas)
342  canvas->redraw ();
343  }
344 
345  void
347  {
348  uipanel::properties& pp = properties<uipanel> ();
349  QFrame* frame = qWidget<QFrame> ();
350 
351  Matrix bb = pp.get_boundingbox (true);
352  int bw = borderWidthFromProperties (pp);
353 
354  frame->setFrameRect (QRect (octave::math::round (bb(0)) - bw,
355  octave::math::round (bb(1)) - bw,
356  octave::math::round (bb(2)) + 2*bw, octave::math::round (bb(3)) + 2*bw));
357  m_container->setGeometry (octave::math::round (bb(0)),
358  octave::math::round (bb(1)),
359  octave::math::round (bb(2)), octave::math::round (bb(3)));
360 
361  if (m_blockUpdates)
362  pp.update_boundingbox ();
363 
364  if (m_title)
365  {
366  QSize sz = m_title->sizeHint ();
367  int offset = 5;
368 
369  if (pp.titleposition_is ("lefttop"))
370  m_title->move (bw+offset, 0);
371  else if (pp.titleposition_is ("righttop"))
372  m_title->move (frame->width () - bw - offset - sz.width (), 0);
373  else if (pp.titleposition_is ("leftbottom"))
374  m_title->move (bw+offset, frame->height () - sz.height ());
375  else if (pp.titleposition_is ("rightbottom"))
376  m_title->move (frame->width () - bw - offset - sz.width (),
377  frame->height () - sz.height ());
378  else if (pp.titleposition_is ("centertop"))
379  m_title->move (frame->width () / 2 - sz.width () / 2, 0);
380  else if (pp.titleposition_is ("centerbottom"))
381  m_title->move (frame->width () / 2 - sz.width () / 2,
382  frame->height () - sz.height ());
383  }
384  }
385 
386 };
static void setupPalette(const uibuttongroup::properties &pp, QPalette &p)
Definition: ButtonGroup.cc:68
void updateLayout(void)
Definition: Panel.cc:346
void update(int pId)
Definition: Panel.cc:232
std::string get_title(void) const
Definition: graphics.h:12857
double get_borderwidth(void) const
Definition: graphics.h:12822
bool is_visible(void) const
Definition: graphics.h:2704
bool eventFilter(QObject *watched, QEvent *event)
Definition: Panel.cc:151
Matrix get_backgroundcolor_rgb(void) const
Definition: graphics.h:12816
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)
bool titleposition_is(const std::string &v) const
Definition: graphics.h:12859
static Object * parentObject(const graphics_object &go)
Definition: Object.cc:165
void redraw(void)
Definition: Panel.cc:337
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:54
double round(double x)
Definition: lo-mappers.cc:333
Matrix get_foregroundcolor_rgb(void) const
Definition: graphics.h:12839
virtual void update_boundingbox(void)
Definition: graphics.cc:3139
static void executeAt(const base_properties &props, const QPoint &pt)
Definition: ContextMenu.cc:113
QColor fromRgb(const Matrix &rgb)
bool bordertype_is(const std::string &v) const
Definition: graphics.h:12819
~Panel(void)
Definition: Panel.cc:147
nd deftypefn *octave_map m
Definition: ov-struct.cc:2058
std::complex< double > w(std::complex< double > z, double relerr=0)
Matrix get_shadowcolor_rgb(void) const
Definition: graphics.h:12854
graphics_handle m_handle
Definition: Object.h:100
is false
Definition: cellfun.cc:398
base_properties & get_properties(void)
Definition: graphics.h:3288
template QFont computeFont< uipanel >(const uipanel::properties &props, int height)
Definition: dMatrix.h:37
sz
Definition: data.cc:5342
QLabel * m_title
Definition: Panel.h:57
virtual QObject * qObject(void)
Definition: Object.h:71
bool m_blockUpdates
Definition: Panel.h:58
bool fontunits_is(const std::string &v) const
Definition: graphics.h:12831
Panel(const graphics_object &go, QFrame *frame)
Definition: Panel.cc:103
graphics_object object(void) const
Definition: Object.cc:72
Matrix get_boundingbox(bool internal=false, const Matrix &parent_pix_size=Matrix()) const
Definition: graphics.cc:9067
octave::unwind_protect frame
Definition: graphics.cc:11584
bool valid_object(void) const
Definition: graphics.h:3306
p
Definition: lu.cc:138
Canvas * canvas(const graphics_handle &handle, bool create=true)
Definition: Container.cc:50
Matrix get_highlightcolor_rgb(void) const
Definition: graphics.h:12844