TitlebarWidget.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package ArmarX::
17 * @author Mirko Waechter ( mirko.waechter at kit dot edu)
18 * @date 2012
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #include "TitlebarWidget.h"
24 #include "WidgetNameDialog.h"
25 
26 
27 #include <QGridLayout>
28 #include <QEvent>
29 #include <QDockWidget>
30 #include <QToolButton>
31 #include <QLabel>
32 #include <QSpacerItem>
33 #include <QToolBar>
34 
36 
37 namespace armarx
38 {
40  QWidget(parent),
41  mainWindow(mainWindow),
42  imageOffline(QPixmap::fromImage(QImage(QString::fromUtf8(":/icons/network-offline.png")))),
43  imageIdle(QPixmap::fromImage(QImage(QString::fromUtf8(":/icons/network-idle.png"))))
44  {
45 
46  this->setContentsMargins(1, 1, 1, 1);
47  dockWidget = qobject_cast<QDockWidget*>(parentWidget());
48  layout = new QGridLayout(this);
49  layout->setMargin(2);
50  layout->setSpacing(2);
51  layout->setContentsMargins(-1, 0, -1, 0);
52 
53  statusIcon = new QLabel(this);
54  statusIcon->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
55  layout->addWidget(statusIcon, 0, 0);
56 
57 
58  title = new QLabel(dockWidget->windowTitle(), this);
59  layout->addWidget(title, 0, 1);
60 
61  editTitleBar = new QToolBar(this);
62  editTitleBar->setIconSize(QSize(16, 16));
63  QIcon icon3;
64  icon3.addFile(QString::fromUtf8(":/icons/accessories-text-editor-6.ico"), QSize(16, 16));
65  QAction* editTitleAction = editTitleBar->addAction(icon3, "");
66  editTitleAction->setToolTip("Change the name of this widget instance");
67  connect(editTitleAction, SIGNAL(triggered(bool)), this, SLOT(openChangeTitleDialog(bool)));
68 
69  layout->addWidget(editTitleBar, 0, 2);
70 
71  QSpacerItem* spacer = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Expanding);
72  layout->addItem(spacer, 0, 4);
73 
74 
75  undockButton = new QToolButton(this);
76  undockButton->setObjectName(QString::fromUtf8("undockButton"));
77  QIcon icon2;
78  icon2.addFile(QString::fromUtf8(":/icons/tab-detach.svg"), QSize(10, 10));
79  undockButton->setIcon(icon2);
80  undockButton->setIconSize(QSize(10, 10));
81  // undockButton->setCheckable(true);
82  undockButton->setToolTip("Toggle the floating status of the widget");
83  layout->addWidget(undockButton, 0, 5);
84  closeButton = new QToolButton(this);
85  closeButton->setObjectName(QString::fromUtf8("btnClose"));
86  QIcon icon1;
87  icon1.addFile(QString::fromUtf8(":/icons/dialog-close.ico"), QSize(8, 8));
88  closeButton->setIcon(icon1);
89  closeButton->setIconSize(QSize(10, 10));
90  closeButton->setToolTip("Close the widget");
91 
92  layout->addWidget(closeButton, 0, 6);
93 
94 
95 
96  connect(closeButton, SIGNAL(clicked()), dockWidget, SLOT(close()));
97  connect(undockButton, SIGNAL(clicked()), this, SLOT(changeFloatingStatus()));
98  }
99 
101  {
102  if (!widget)
103  {
104  return;
105  }
106 
107  if (!widget->parent())
108  {
109  widget->setParent(this);
110  }
111 
112  layout->addWidget(widget, 0, 3);
113 
114  }
115 
117  {
118  QDockWidget* dockWidget = qobject_cast<QDockWidget*>(parentWidget());
119  QSize size;
120  // size = dockWidget->size();
121  size.setHeight(0);
122  size.setWidth(0);
123 
124 
125  if (dockWidget->features() & QDockWidget::DockWidgetVerticalTitleBar)
126  {
127  // I need to be vertical
128  size.setHeight(title->minimumWidth() + 30);
129  size.setWidth(34);
130  }
131  else
132  {
133  // I need to be horizontal
134  return layout->sizeHint();
135 
136  }
137 
138  return size;
139  }
140 
142  {
143  QDockWidget* dockWidget = qobject_cast<QDockWidget*>(parentWidget());
144  QSize size;
145 
146  size.setHeight(0);
147 
148  if (dockWidget->features() & QDockWidget::DockWidgetVerticalTitleBar)
149  {
150  // I need to be vertical
151  size.setHeight(layout->minimumSize().height());
152 
153  size.setWidth(30);
154  }
155  else
156  {
157  // I need to be horizontal
158  size.setHeight(30);
159 
160  size.setWidth(layout->minimumSize().width());
161 
162  }
163 
164  return size;
165  }
166 
168  {
169  event->ignore();
170  return false;
171  }
172 
174  {
175  QDockWidget* dockWidget = qobject_cast<QDockWidget*>(parentWidget());
176  // undockButton->setChecked(!dockWidget->isFloating());
177  dockWidget->setFloating(!dockWidget->isFloating());
178  }
179 
180  void StatusDockWidgetTitleBar::changeStatus(ManagedIceObjectState state, QStringList dependencies)
181  {
182  auto setTextColor = [this](QString color)
183  {
184  title->setStyleSheet("QLabel { color : " + color + "; }");
185  };
186  setTextColor("black");
187  QDockWidget* dockWidget = qobject_cast<QDockWidget*>(parentWidget());
188  title->setText(dockWidget->windowTitle());
189  switch (state)
190  {
191  case eManagedIceObjectCreated:
192  statusIcon->setToolTip("Component created");
193  break;
194 
195  case eManagedIceObjectInitializing:
196  statusIcon->setToolTip("Component initializing");
197  setTextColor("yellow");
198  break;
199 
200  case eManagedIceObjectInitialized:
201  if (dependencies.length() > 0)
202  {
203  QString waitingFor = "Component initialized & waiting for dependencies: " + dependencies.join(", ");
204  statusIcon->setToolTip(waitingFor);
205  title->setToolTip(waitingFor);
206  title->setText(dockWidget->windowTitle() + " waiting for " + dependencies.at(0) + (dependencies.length() > 1 ? " ..." : ""));
207  setTextColor("red");
208  }
209  else
210  {
211  setTextColor("black");
212  statusIcon->setToolTip("Component initialized");
213  }
214 
215  break;
216 
217  case eManagedIceObjectExited:
218  statusIcon->setToolTip("Component exited");
219  break;
220 
221  case eManagedIceObjectExiting:
222  statusIcon->setToolTip("Component exiting");
223  break;
224 
225  case eManagedIceObjectStarting:
226  statusIcon->setToolTip("Component starting");
227  setTextColor("yellow");
228 
229  break;
230 
231  case eManagedIceObjectStarted:
232  if (dependencies.length() > 0)
233  {
234  QString connectedTo = "Component started & connected to " + dependencies.join(", ");
235  statusIcon->setToolTip(connectedTo);
236  title->setToolTip(connectedTo);
237  }
238  else
239  {
240  statusIcon->setToolTip("Component started");
241  }
242 
243  break;
244 
245  default:
246  break;
247  }
248 
249  switch (state)
250  {
251  case eManagedIceObjectStarted:
252  {
253  statusIcon->setPixmap(imageIdle);
254  }
255  break;
256 
257  default:
258  {
259  statusIcon->setPixmap(imageOffline);
260  }
261  break;
262  }
263  }
264 
266  {
267  WidgetNameDialog d(dockWidget->objectName(), mainWindow);
268 
269  if (d.exec() == QDialog::Accepted)
270  {
271  dockWidget->setWindowTitle(d.getWidgetName());
272  dockWidget->setObjectName(d.getWidgetName());
273  title->setText(d.getWidgetName());
274  }
275  }
276 }
armarx::ArmarXMainWindow
The ArmarXMainWindow class.
Definition: ArmarXMainWindow.h:76
armarx::StatusDockWidgetTitleBar::event
bool event(QEvent *event) override
Definition: TitlebarWidget.cpp:167
WidgetNameDialog.h
armarx::StatusDockWidgetTitleBar::StatusDockWidgetTitleBar
StatusDockWidgetTitleBar(QWidget *parent=0, ArmarXMainWindow *mainWindow=0)
Definition: TitlebarWidget.cpp:39
armarx::StatusDockWidgetTitleBar::addCustomWidget
void addCustomWidget(QWidget *widget)
Definition: TitlebarWidget.cpp:100
armarx::StatusDockWidgetTitleBar::changeStatus
void changeStatus(ManagedIceObjectState state, QStringList dependencies)
Definition: TitlebarWidget.cpp:180
armarx::StatusDockWidgetTitleBar::changeFloatingStatus
void changeFloatingStatus()
Definition: TitlebarWidget.cpp:173
ArmarXMainWindow.h
armarx::WidgetNameDialog
The WidgetNameDialog class.
Definition: WidgetNameDialog.h:42
armarx::WidgetNameDialog::getWidgetName
QString getWidgetName() const
Definition: WidgetNameDialog.cpp:58
armarx::StatusDockWidgetTitleBar::minimumSizeHint
QSize minimumSizeHint() const override
Definition: TitlebarWidget.cpp:141
TitlebarWidget.h
armarx::StatusDockWidgetTitleBar::openChangeTitleDialog
void openChangeTitleDialog(bool)
Definition: TitlebarWidget.cpp:265
armarx::StatusDockWidgetTitleBar::sizeHint
QSize sizeHint() const override
Definition: TitlebarWidget.cpp:116
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28