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
Panel.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2011-2015 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 #ifdef 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  {
79  bw = xround (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 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 (xround (bb(0)), xround (bb(1)),
112  xround (bb(2)), xround (bb(3)));
113  frame->setFrameStyle (frameStyleFromProperties (pp));
114  frame->setLineWidth (xround (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 
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  ContextMenu::executeAt (properties (), m->globalPos ());
195  }
196  }
197  break;
198 
199  default:
200  break;
201  }
202  }
203  else if (watched == m_container)
204  {
205  switch (xevent->type ())
206  {
207  case QEvent::Resize:
208  if (qWidget<QWidget> ()->isVisible ())
209  {
211 
213  }
214  break;
215 
216  default:
217  break;
218  }
219  }
220  }
221 
222  return false;
223 }
224 
225 void
226 Panel::update (int pId)
227 {
228  uipanel::properties& pp = properties<uipanel> ();
229  QFrame* frame = qWidget<QFrame> ();
230 
231  m_blockUpdates = true;
232 
233  switch (pId)
234  {
236  {
237  Matrix bb = pp.get_boundingbox (false);
238 
239  frame->setGeometry (xround (bb(0)), xround (bb(1)),
240  xround (bb(2)), xround (bb(3)));
241  updateLayout ();
242  }
243  break;
244 
246  frame->setLineWidth (xround (pp.get_borderwidth ()));
247  updateLayout ();
248  break;
249 
254  {
255  QPalette pal = frame->palette ();
256 
257  setupPalette (pp, pal);
258  frame->setPalette (pal);
259  if (m_title)
260  m_title->setPalette (pal);
261  }
262  break;
263 
265  {
266  QString title = Utils::fromStdString (pp.get_title ());
267 
268  if (title.isEmpty ())
269  {
270  if (m_title)
271  delete m_title;
272  m_title = 0;
273  }
274  else
275  {
276  if (! m_title)
277  {
278  QPalette pal = frame->palette ();
279 
280  m_title = new QLabel (title, frame);
281  m_title->setAutoFillBackground (true);
282  m_title->setContentsMargins (4, 0, 4, 0);
283  m_title->setPalette (pal);
284  m_title->setFont (Utils::computeFont<uipanel> (pp));
285  m_title->show ();
286  }
287  else
288  {
289  m_title->setText (title);
290  m_title->resize (m_title->sizeHint ());
291  }
292  }
293  updateLayout ();
294  }
295  break;
296 
298  updateLayout ();
299  break;
300 
302  frame->setFrameStyle (frameStyleFromProperties (pp));
303  updateLayout ();
304  break;
305 
310  if (m_title)
311  {
312  m_title->setFont (Utils::computeFont<uipanel> (pp));
313  m_title->resize (m_title->sizeHint ());
314  updateLayout ();
315  }
316  break;
317 
319  frame->setVisible (pp.is_visible ());
320  updateLayout ();
321  break;
322 
323  default:
324  break;
325  }
326 
327  m_blockUpdates = false;
328 }
329 
330 void
332 {
333  Canvas* canvas = m_container->canvas (m_handle);
334 
335  if (canvas)
336  canvas->redraw ();
337 }
338 
339 void
341 {
342  uipanel::properties& pp = properties<uipanel> ();
343  QFrame* frame = qWidget<QFrame> ();
344 
345  Matrix bb = pp.get_boundingbox (true);
346  int bw = borderWidthFromProperties (pp);
347 
348  frame->setFrameRect (QRect (xround (bb(0)) - bw, xround (bb(1)) - bw,
349  xround (bb(2)) + 2*bw, xround (bb(3)) + 2*bw));
350  m_container->setGeometry (xround (bb(0)), xround (bb(1)),
351  xround (bb(2)), xround (bb(3)));
352 
353  if (m_blockUpdates)
354  pp.update_boundingbox ();
355 
356  if (m_title)
357  {
358  QSize sz = m_title->sizeHint ();
359  int offset = 5;
360 
361  if (pp.titleposition_is ("lefttop"))
362  m_title->move (bw+offset, 0);
363  else if (pp.titleposition_is ("righttop"))
364  m_title->move (frame->width () - bw - offset - sz.width (), 0);
365  else if (pp.titleposition_is ("leftbottom"))
366  m_title->move (bw+offset, frame->height () - sz.height ());
367  else if (pp.titleposition_is ("rightbottom"))
368  m_title->move (frame->width () - bw - offset - sz.width (),
369  frame->height () - sz.height ());
370  else if (pp.titleposition_is ("centertop"))
371  m_title->move (frame->width () / 2 - sz.width () / 2, 0);
372  else if (pp.titleposition_is ("centerbottom"))
373  m_title->move (frame->width () / 2 - sz.width () / 2,
374  frame->height () - sz.height ());
375  }
376 }
377 
378 };
void updateLayout(void)
Definition: Panel.cc:340
void update(int pId)
Definition: Panel.cc:226
std::string get_title(void) const
Definition: graphics.h:12240
double get_borderwidth(void) const
Definition: graphics.h:12205
bool is_visible(void) const
Definition: graphics.h:2758
double xround(double x)
Definition: lo-mappers.cc:63
bool eventFilter(QObject *watched, QEvent *event)
Definition: Panel.cc:152
Matrix get_backgroundcolor_rgb(void) const
Definition: graphics.h:12199
static int frameStyleFromProperties(const uipanel::properties &pp)
Definition: Panel.cc:43
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:12242
static Object * parentObject(const graphics_object &go)
Definition: Object.cc:170
void redraw(void)
Definition: Panel.cc:331
virtual Container * innerContainer(void)=0
Container * m_container
Definition: Panel.h:56
void redraw(bool sync=false)
Definition: Canvas.cc:53
Matrix get_foregroundcolor_rgb(void) const
Definition: graphics.h:12222
virtual void update_boundingbox(void)
Definition: graphics.cc:3134
static void executeAt(const base_properties &props, const QPoint &pt)
Definition: ContextMenu.cc:114
QColor fromRgb(const Matrix &rgb)
bool bordertype_is(const std::string &v) const
Definition: graphics.h:12202
~Panel(void)
Definition: Panel.cc:147
std::complex< double > w(std::complex< double > z, double relerr=0)
Matrix get_shadowcolor_rgb(void) const
Definition: graphics.h:12237
graphics_handle m_handle
Definition: Object.h:100
template QFont computeFont< uipanel >(const uipanel::properties &props, int height)
Definition: dMatrix.h:35
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:12214
static void setupPalette(const uipanel::properties &pp, QPalette &p)
Definition: Panel.cc:60
Panel(const graphics_object &go, QFrame *frame)
Definition: Panel.cc:103
graphics_object object(void) const
Definition: Object.cc:73
Matrix get_boundingbox(bool internal=false, const Matrix &parent_pix_size=Matrix()) const
Definition: graphics.cc:9028
bool valid_object(void) const
Definition: graphics.h:3395
static int borderWidthFromProperties(const uipanel::properties &pp)
Definition: Panel.cc:73
Canvas * canvas(const graphics_handle &handle, bool create=true)
Definition: Container.cc:51
Matrix get_highlightcolor_rgb(void) const
Definition: graphics.h:12227
base_properties & properties(void)
Definition: Object.h:49