rock_widget_collection  0.1
Public Slots | Signals | Public Member Functions | Protected Attributes | List of all members
MultiViewWidget Class Reference

#include <MultiViewWidget.h>

Inherits QWidget.

Public Slots

void updateView ()
 
void addWidget (const QString &name, QWidget *widget, const QIcon &icon=QIcon(), bool useOnlyIcon=false, int position=0)
 
QWidget * getWidget (const QString &name)
 
void deleteWidget (const QString &name)
 
void widgetClicked ()
 
void setThumbnailSize (int width=150, int height=150)
 
WidgetButtongetButtonForWidget (QWidget *widget)
 
QList< WidgetButton * > getAllWidgetButtons ()
 
void setDesignerMode (bool designerMode)
 

Signals

void widgetButtonAdded (QWidget *widget)
 

Public Member Functions

 MultiViewWidget (QWidget *parent)
 
virtual ~MultiViewWidget ()
 
void childEvent (QChildEvent *event)
 
bool event (QEvent *event)
 
bool isDesignerMode ()
 
void fixStatus ()
 

Protected Attributes

QHash< QString, WidgetButton * > widgets
 
QGridLayout layout
 
QHBoxLayout * top
 
QHBoxLayout * bottom
 
QVBoxLayout * left
 
QVBoxLayout * mid
 
QVBoxLayout * right
 
WidgetButtoncurrentWidget
 
int thumbnailWidth
 
int thumbnailHeight
 
QWidget * pseudoWidget
 
bool initialized
 
int currentWidgetIndex
 
bool clicking
 
bool designerMode
 
QTimer timer
 

Detailed Description

Widget which displays other widgets. All added widgets will be displayed as thumbnails either left, right, on top or below one widget. Thumbnail Widgets can be clicked to show in the main potion of the screen. S one widget will be the "active" widget which takes most of the sapce within the widget all other widgets will be displayed as small widget in one of the positions as mentioned above.
If there are any changes to an added widget, the thumnail will blink in red. This only works if the added widget has a notifyUpdate(int) signal. The signal will automatically be taken into account via the qt meta object infrastructure. The currently selected widget will be displayed with a name and is colorized in green to indicate selection.
The size and orientation of the thumbnails can be configurede via the methods setThumbnailPosition(int) and setThumnailSize(int, int).
If the widget is too time consuming or can not be properly displayed when being small one can either only show the widgets name or alternatively an icon.
The widget will try to resize teh viewable center widget as large as it can. If this is not the desired behaviour setting a fixed or maximum size will work properly, so the widget will not stretch to more than the given sizes.

Author
Bjoern Lueck bluec.nosp@m.k@df.nosp@m.ki.de
Version
0.1

Definition at line 45 of file MultiViewWidget.h.

Constructor & Destructor Documentation

MultiViewWidget::MultiViewWidget ( QWidget *  parent)

Constructor

Parameters
parentparentWidget will be given to QWidgets constructor

Definition at line 24 of file MultiViewWidget.cc.

24  : QWidget(parent)
25 {
26  initialized = false;
27  currentWidget = NULL;
28  pseudoWidget = NULL;
29  designerMode = false;
30  clicking = false;
32  thumbnailWidth = 150;
33  thumbnailHeight = 150;
34 
35 
36  top = new QHBoxLayout();
37  bottom = new QHBoxLayout();
38  left = new QVBoxLayout();
39  mid = new QVBoxLayout();
40  right = new QVBoxLayout();
41  setLayout(&layout);
42  initialized=true;
43 
44 
45  layout.update();
46  layout.activate();
47 
48 }
QVBoxLayout * right
QHBoxLayout * top
QHBoxLayout * bottom
QVBoxLayout * left
QVBoxLayout * mid
QWidget * pseudoWidget
QGridLayout layout
WidgetButton * currentWidget
MultiViewWidget::~MultiViewWidget ( )
virtual

Destructor

Todo:
implement correctly

Definition at line 50 of file MultiViewWidget.cc.

51 {
52 }

Member Function Documentation

void MultiViewWidget::addWidget ( const QString &  name,
QWidget *  widget,
const QIcon &  icon = QIcon(),
bool  useOnlyIcon = false,
int  position = 0 
)
slot

Adds a widget and a thumbnail to the widget

Parameters
namethe name given to the widget, must be unique.
widgetteh widget to add
iconan icon as alternative for text or the wisget itself
useOnlyIconif only the icon and or text shall be displayed as thumbnail and NOT the widget

Definition at line 116 of file MultiViewWidget.cc.

117 {
118  QWidget *widget = widget_;
119 
120 #if 0 //Not possible because ruby qt don't initialize all components as QWidget so we have no chance to get the right obect at this point
121  //Try cast the QT way
122  MultiWidget *w = qobject_cast<MultiWidget*>(widget_);
123  if(w){
124  widget= w;
125  printf("Cast sucsessful\n");
126  }else{
127  //MultiWidget *bla = (MultiWidget*) widget_;
128  printf("ARGHHHH: %s Name: %s \n",typeid(widget).name(),widget->objectName().toStdString().c_str());
129 
130  printf("QT Means: %s\n",widget->metaObject()->className());
131  const QMetaObject *o = widget->metaObject();
132  for(int i=0; i<o->methodCount(); i++){
133  printf("Method found: %s\n",o->method(i).signature());
134  }
135  printf("\n\n");
136  //printf("Got one: %s \n",bla->getMinimizedLabel().toStdString().c_str());
137  //printf("Cast failed\n");
138  }
139 #endif
140 
141 
142  if(widgets.contains(name))
143  {
144  std::cerr << "A Widget with the name: [" << name.toStdString() << "] already exists!" << std::endl;
145  return;
146  }
147  WidgetButton* widgetButton=0;
148 
149  switch(position){
150  case WidgetButton::Left:
151  case WidgetButton::Right:
152  widgetButton = new WidgetButton(0,Qt::Vertical,thumbnailWidth, thumbnailHeight);
153  break;
154  case WidgetButton::Top:
156  default:
157  widgetButton = new WidgetButton(0,Qt::Horizontal,thumbnailWidth, thumbnailHeight);
158  break;
159  }
160 
161  //widgetButton->setFixedSize(thumbnailWidth, thumbnailHeight);
162  widgetButton->setIconAlternative(icon, useOnlyIcon);
163 
164 
165  // show the widget if its the only one
166  if(widgets.size() == 0)
167  {
168  widgetButton->setWidget(name, widget, false);
169  widgets.insert(name, widgetButton);
170  mid->addWidget(widget);
171  currentWidget = widgetButton;
172  }
173  else
174  {
175  widgetButton->setWidget(name, widget);
176  }
177 
178  widgets.insert(name, widgetButton);
179  //left->addWidget(widgetButton);
180 
181 
182  if(position != WidgetButton::Default){
183  switch(position){
184  case WidgetButton::Left:
185  left->addWidget(widgetButton);
186  break;
187  case WidgetButton::Right:
188  right->addWidget(widgetButton);
189  break;
190  case WidgetButton::Top:
191  top->addWidget(widgetButton);
192  break;
194  bottom->addWidget(widgetButton);
195  break;
196  }
197  }else{
198  switch(currentWidget->getPosition())
199  {
200  case WidgetButton::Left:
201  left->addWidget(widgetButton);
202  break;
203  case WidgetButton::Right:
204  right->addWidget(widgetButton);
205  break;
207  bottom->addWidget(widgetButton);
208  break;
209  case WidgetButton::Top:
210  top->addWidget(widgetButton);
211  break;
212  }
213  }
214 
215  connect(widgetButton, SIGNAL(clicked()), this, SLOT(widgetClicked()));
216  emit widgetButtonAdded(widgetButton);
217  updateView();
218 
219  timer.setSingleShot(true);
220  timer.start(500);
221  connect(&timer,SIGNAL(timeout()),this,SLOT(fixStatus()));
222 }
QHash< QString, WidgetButton * > widgets
QVBoxLayout * right
QHBoxLayout * top
QHBoxLayout * bottom
void widgetButtonAdded(QWidget *widget)
QVBoxLayout * left
QVBoxLayout * mid
void setIconAlternative(const QIcon &icon, bool isAlternative=false)
void setWidget(const QString &name, QWidget *widget, bool shown=true)
const Position getPosition() const
Definition: WidgetButton.cc:53
WidgetButton * currentWidget
void MultiViewWidget::childEvent ( QChildEvent *  event)

Handles the events when a widget is added to or removed from the widget. This will not do anything when used in standalone mode, but will be used heavily when used in the qt designer.
When adding a widget to or removing a widget from the MultiViewWidget from within QTDesigner the childEvent method will map them to the corresponding methods within the MultiViewWidget. Thus enabling the widget to perform within the qt designer as container with teh desired functionality.
Note the only thing which does not work is the thumbnail widgets. Widgets will not be displayed as thumbnails within the designer, as the designer needs ownership of those. In Designer's preview everything will act asexpected

Definition at line 271 of file MultiViewWidget.cc.

272 {
273  //QWidget::childEvent(event);
274  if(initialized)
275  {
276 
277  if(event->type() == QChildEvent::ChildAdded)
278  {
279  if(!clicking)
280  {
281  if(event->child()->isWidgetType())
282  {
283  // we never need to watch adding of widgetbuttons, then everything went well already
284  WidgetButton* testButton = dynamic_cast<WidgetButton*> (event->child());
285  if(testButton != NULL)
286  {
287  return;
288  }
289  // we never want to add a widget which is already added
290  QWidget* child = (QWidget*)event->child();
291  if(child->windowTitle().isEmpty())
292  addWidget(QString::number(currentWidgetIndex), child);
293  else
294  addWidget(child->windowTitle(), child);
296  }
297  }
298  }
299  else if(event->type() == QChildEvent::ChildRemoved)
300  {
301  if(!clicking)
302  {
303  if(event->child()->isWidgetType())
304  {
305  // look which widget was removed and remove the button also
306  QWidget* child = (QWidget*)event->child();
307  QList<QString> keys = widgets.keys();
308  for(int i=0;i<keys.size();i++)
309  {
310  WidgetButton* button = widgets[keys[i]];
311  if(button->getWidget() == child)
312  {
313  widgets.remove(keys[i]);
314  QWidget* widget = button->getWidget();
315  layout.removeWidget(widget);
316  mid->removeWidget(button);
317  left->removeWidget(button);
318  right->removeWidget(button);
319  bottom->removeWidget(button);
320  top->removeWidget(button);
321  widget = NULL;
322  return;
323  }
324  }
325  }
326  }
327  }
328  }
329 }
QHash< QString, WidgetButton * > widgets
QVBoxLayout * right
QHBoxLayout * top
QHBoxLayout * bottom
QVBoxLayout * left
QVBoxLayout * mid
bool event(QEvent *event)
void addWidget(const QString &name, QWidget *widget, const QIcon &icon=QIcon(), bool useOnlyIcon=false, int position=0)
QGridLayout layout
QWidget * getWidget()
void MultiViewWidget::deleteWidget ( const QString &  name)
slot

Removes the widget with the given name. This will DELETE the widget that was added with that name!!

Parameters
namethe anme of the widget to remove

Definition at line 242 of file MultiViewWidget.cc.

243 {
244  if(widgets.contains(name))
245  {
246  WidgetButton* button = widgets[name];
247  widgets.remove(name);
248  QWidget* widget = button->getWidget();
249  layout.removeWidget(widget);
250  mid->removeWidget(button);
251  top->removeWidget(button);
252  left->removeWidget(button);
253  right->removeWidget(button);
254  bottom->removeWidget(button);
255  delete button;
256  }
257  updateView();
258 }
QHash< QString, WidgetButton * > widgets
QVBoxLayout * right
QHBoxLayout * top
QHBoxLayout * bottom
QVBoxLayout * left
QVBoxLayout * mid
QGridLayout layout
QWidget * getWidget()
bool MultiViewWidget::event ( QEvent *  event)

Definition at line 260 of file MultiViewWidget.cc.

261 {
262  if(event->type() == QEvent::ToolTip)
263  {
264  QToolTip::hideText();
265  event->ignore();
266  return true;
267  }
268  return QWidget::event(event);
269 }
bool event(QEvent *event)
void MultiViewWidget::fixStatus ( )

Fix the Status (names and view Style of all Widgets, this cannot be done during adding because rubyqt says all widets are QWidget

Definition at line 224 of file MultiViewWidget.cc.

224  {
225  QList<WidgetButton*> list = getAllWidgetButtons();
226  for(int i=0;i<list.size();i++){
227  if(list[i] == currentWidget){
228  list[i]->setActive(true);
229  }else{
230  list[i]->setActive(false);
231  }
232  list[i]->corrcetName();
233  }
234 }
QList< WidgetButton * > getAllWidgetButtons()
WidgetButton * currentWidget
QList< WidgetButton * > MultiViewWidget::getAllWidgetButtons ( )
slot

Returns all Buttons

Returns
all Buttons as QList

Definition at line 331 of file MultiViewWidget.cc.

332 {
333  return widgets.values();
334 }
QHash< QString, WidgetButton * > widgets
WidgetButton * MultiViewWidget::getButtonForWidget ( QWidget *  widget)
slot

Returns the widget which is shown on the Button

Parameters
widgetthe widget for which the button shall be returned
Returns
the button for the given widget

Definition at line 102 of file MultiViewWidget.cc.

103 {
104  QList<QString> keys = widgets.keys();
105  for(int i=0;i<keys.size();i++)
106  {
107  WidgetButton* button = widgets[keys[i]];
108  if(button->getWidget() == widget)
109  {
110  return button;
111  }
112  }
113  return NULL;
114 }
QHash< QString, WidgetButton * > widgets
QWidget * getWidget()
QWidget * MultiViewWidget::getWidget ( const QString &  name)
slot

Return sthe widget for the given name

Parameters
namethe name of the widget to return given when added via addWidget
Returns
the widget which was added with the given name
Todo:
implement correctly

Definition at line 237 of file MultiViewWidget.cc.

238 {
239  return widgets.value(name)->getWidget();
240 }
QHash< QString, WidgetButton * > widgets
bool MultiViewWidget::isDesignerMode ( )
inline

Returns whether designer mode is enabled

Returns
if designer mode is enabled

Definition at line 146 of file MultiViewWidget.h.

146 { return designerMode; };
void MultiViewWidget::setDesignerMode ( bool  designerMode)
inlineslot

Sets whether designer mode is enabled (no small widgets) NOTE: You should normally never have to call this manually

Parameters
designerModetrue if the designer is shown, false otherwise

Definition at line 140 of file MultiViewWidget.h.

140 { this->designerMode = designerMode; };
void MultiViewWidget::setThumbnailSize ( int  width = 150,
int  height = 150 
)
slot

Sets the size of teh thumbnail widgets. Defaults the thumbnails to 150x150.

Parameters
widthteh width of the thumbnails, defaults to 150
heightthe height of the thumbnails defaults to 150

Definition at line 96 of file MultiViewWidget.cc.

97 {
98  thumbnailWidth = width;
99  thumbnailHeight = height;
100 }
void MultiViewWidget::updateView ( )
slot

Definition at line 55 of file MultiViewWidget.cc.

55  {
56  int midStart = 1;
57  int centerWidth = 3;
58 
59  if(top->count() == 0){
60  layout.removeItem(top);
61  midStart-=1;
62  }else{
63  centerWidth++;
64  layout.addLayout(top,0,0,1,3);
65  }
66 
67 
68  if(right->count() > 0){
69  centerWidth--;
70  layout.removeItem(right);
71  layout.addLayout(right,midStart,2,1,1);
72  }else{
73  layout.removeItem(right);
74  }
75 
76  if(left->count() > 0){
77  layout.removeItem(mid);
78  centerWidth--;
79  layout.addLayout(left,midStart,0,1,1);
80  layout.addLayout(mid,midStart,1,1,centerWidth);
81  }else{
82  layout.removeItem(left);
83  layout.removeItem(mid);
84  layout.addLayout(mid,midStart,0,1,centerWidth);
85  }
86 
87 
88  if(bottom->count() > 0){
89  layout.addLayout(bottom,midStart+1,0,1,3);
90  }else{
91  layout.removeItem(bottom);
92  }
93  layout.update();
94 }
QVBoxLayout * right
QHBoxLayout * top
QHBoxLayout * bottom
QVBoxLayout * left
QVBoxLayout * mid
QGridLayout layout
void MultiViewWidget::widgetButtonAdded ( QWidget *  widget)
signal

Emitted when a new widget is added

Parameters
widgetthe widget which was added to the MultiWidget
void MultiViewWidget::widgetClicked ( )
slot

Called when a thumbnail was clicked

Definition at line 336 of file MultiViewWidget.cc.

337 {
338  clicking = true;
339 
340  WidgetButton *sender = dynamic_cast<WidgetButton*>(QObject::sender());
341  if(sender <= 0){
342  fprintf(stderr,"FATAL: Cannot Handle unknown Widget %s:%i\n",__FILE__,__LINE__);
343  return;
344  }
345 
346  //sender->printStatus();
347  sender->showWidget(false);
348  QWidget* widget = sender->getWidget();
349 /*
350  if(designerMode)
351  {
352  QList<WidgetButton*> buttons = getAllWidgetButtons();
353  for(int i=0;i<buttons.size();i++)
354  {
355  if(sender != buttons[i])
356  {
357  buttons[i]->getWidget()->setMaximumSize(0, 0);
358  }
359  else
360  {
361  buttons[i]->getWidget()->setMaximumSize(1000000, 1000000);
362  }
363 
364  }
365  }
366  */
367  widget->setEnabled(true);
368 
369  currentWidget->setActive(false);
370  sender->setActive(true);
371 
372  mid->removeWidget(currentWidget->getWidget());
373  mid->addWidget(widget);
374 
375  currentWidget = sender;
376  QList<QString> keys = widgets.keys();
377  for(int i=0;i<keys.size();i++)
378  {
379  WidgetButton* button = widgets.value(keys[i]);
380  if(button != sender)
381  {
382  button->showWidget(true);
383  }
384  }
385 
386  /*
387  // try to be stupid and use an arbitary large number
388  // this is NOT a good solution, if you've got a better one....
389  if(currentWidget->getWidget()->maximumHeight() < 1000000 || currentWidget->getWidget()->maximumWidth() < 1000000)
390  {
391  // no breaks in left and right are ON PURPOSE
392  switch(currentWidget->getPosition())
393  {
394  case WidgetButton::Left:
395  {
396  center.setColumnStretch(3, 10);
397  }
398  case WidgetButton::Right:
399  {
400  layout.setColumnStretch(0, 10);
401  }
402  case WidgetButton::Top:
403  {
404  layout.setRowStretch(3, 10);
405  break;
406  }
407  case WidgetButton::Bottom:
408  {
409  layout.setRowStretch(0, 10);
410  break;
411  }
412  }
413  }
414  else
415  {
416  switch(currentWidget->getPosition())
417  {
418  case WidgetButton::Left:
419  {
420  layout.setColumnStretch(3, 0);
421  }
422  case WidgetButton::Right:
423  {
424  layout.setColumnStretch(0, 0);
425  }
426  case WidgetButton::Top:
427  {
428  layout.setRowStretch(3, 0);
429  break;
430  }
431  case WidgetButton::Bottom:
432  {
433  layout.setRowStretch(0, 0);
434  break;
435  }
436  }
437  }
438  */
439  clicking = false;
440 // repaint();
441 }
QHash< QString, WidgetButton * > widgets
QVBoxLayout * mid
void setActive(bool active)
Definition: WidgetButton.cc:64
WidgetButton * currentWidget
void showWidget(bool shown)
QWidget * getWidget()

Member Data Documentation

QHBoxLayout * MultiViewWidget::bottom
protected

Definition at line 176 of file MultiViewWidget.h.

bool MultiViewWidget::clicking
protected

if a widget was clicked and the widgetClicked method is currently processing

Definition at line 194 of file MultiViewWidget.h.

WidgetButton* MultiViewWidget::currentWidget
protected

The widget currently displayed in the center part of the widget

Definition at line 180 of file MultiViewWidget.h.

int MultiViewWidget::currentWidgetIndex
protected

The current index for use with qt designer. automatically gives numbers instead of names

Definition at line 192 of file MultiViewWidget.h.

bool MultiViewWidget::designerMode
protected

Whether the designer mode is enabled

Definition at line 196 of file MultiViewWidget.h.

bool MultiViewWidget::initialized
protected

Whether the view is already initialized

Definition at line 190 of file MultiViewWidget.h.

QGridLayout MultiViewWidget::layout
protected

Main layout

Definition at line 174 of file MultiViewWidget.h.

QVBoxLayout* MultiViewWidget::left
protected

Definition at line 177 of file MultiViewWidget.h.

QVBoxLayout * MultiViewWidget::mid
protected

Definition at line 177 of file MultiViewWidget.h.

QWidget* MultiViewWidget::pseudoWidget
protected

Widget for Layouting purposes

Definition at line 187 of file MultiViewWidget.h.

QVBoxLayout * MultiViewWidget::right
protected

Definition at line 177 of file MultiViewWidget.h.

int MultiViewWidget::thumbnailHeight
protected

The height of thumbnails

Definition at line 185 of file MultiViewWidget.h.

int MultiViewWidget::thumbnailWidth
protected

The width of thumbnails

Definition at line 183 of file MultiViewWidget.h.

QTimer MultiViewWidget::timer
protected

Workaround to fix Ruby initialization, the widgets are not recognized well

Definition at line 199 of file MultiViewWidget.h.

QHBoxLayout* MultiViewWidget::top
protected

Definition at line 176 of file MultiViewWidget.h.

QHash<QString, WidgetButton*> MultiViewWidget::widgets
protected

Adds the menu to the widget, currently its only use is to have a testing environmentAll thumbnails and widgets added

Definition at line 172 of file MultiViewWidget.h.


The documentation for this class was generated from the following files: