FloatingWidgetTitleBar.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  ** Qt Advanced Docking System
3  ** Copyright (C) 2017 Uwe Kindler
4  **
5  ** This library is free software; you can redistribute it and/or
6  ** modify it under the terms of the GNU Lesser General Public
7  ** License as published by the Free Software Foundation; either
8  ** version 2.1 of the License, or (at your option) any later version.
9  **
10  ** This library is distributed in the hope that it will be useful,
11  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  ** Lesser General Public License for more details.
14  **
15  ** You should have received a copy of the GNU Lesser General Public
16  ** License along with this library; If not, see <http://www.gnu.org/licenses/>.
17  ******************************************************************************/
18 
19 //============================================================================
24 //============================================================================
25 
26 //============================================================================
27 // INCLUDES
28 //============================================================================
29 #include "FloatingWidgetTitleBar.h"
30 
31 #include <iostream>
32 
33 #include <QHBoxLayout>
34 #include <QPushButton>
35 #include <QToolButton>
36 #include <QPixmap>
37 #include <QStyle>
38 #include <QMouseEvent>
39 
40 #include "ads_globals.h"
41 #include "ElidingLabel.h"
42 #include "FloatingDockContainer.h"
43 
44 namespace ads
45 {
46 
47 using tTabLabel = CElidingLabel;
48 using tCloseButton = QToolButton;
49 using tMaximizeButton = QToolButton;
50 
55 {
57  QLabel *IconLabel = nullptr;
63  QIcon MaximizeIcon;
64  QIcon NormalIcon;
65  bool Maximized = false;
66 
68  _this(_public)
69  {
70  }
71 
75  void createLayout();
76 };
77 
78 //============================================================================
80 {
81  TitleLabel = new tTabLabel();
82  TitleLabel->setElideMode(Qt::ElideRight);
83  TitleLabel->setText("DockWidget->windowTitle()");
84  TitleLabel->setObjectName("floatingTitleLabel");
85  TitleLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
86 
87  CloseButton = new tCloseButton();
88  CloseButton->setObjectName("floatingTitleCloseButton");
89  CloseButton->setAutoRaise(true);
90 
92  MaximizeButton->setObjectName("floatingTitleMaximizeButton");
93  MaximizeButton->setAutoRaise(true);
94 
95  // The standard icons do does not look good on high DPI screens
96  QIcon CloseIcon;
97  QPixmap normalPixmap = _this->style()->standardPixmap(
98  QStyle::SP_TitleBarCloseButton, 0, CloseButton);
99  CloseIcon.addPixmap(normalPixmap, QIcon::Normal);
100  CloseIcon.addPixmap(internal::createTransparentPixmap(normalPixmap, 0.25),
101  QIcon::Disabled);
102  CloseButton->setIcon(
103  _this->style()->standardIcon(QStyle::SP_TitleBarCloseButton));
104  CloseButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
105  CloseButton->setVisible(true);
106  CloseButton->setFocusPolicy(Qt::NoFocus);
107  _this->connect(CloseButton, SIGNAL(clicked()), SIGNAL(closeRequested()));
108 
109  _this->setMaximizedIcon(false);
110  MaximizeButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
111  MaximizeButton->setVisible(true);
112  MaximizeButton->setFocusPolicy(Qt::NoFocus);
113  _this->connect(MaximizeButton, &QPushButton::clicked, _this, &CFloatingWidgetTitleBar::maximizeRequested);
114 
115  QFontMetrics fm(TitleLabel->font());
116  int Spacing = qRound(fm.height() / 4.0);
117 
118  // Fill the layout
119  QBoxLayout *Layout = new QBoxLayout(QBoxLayout::LeftToRight);
120  Layout->setContentsMargins(6, 0, 0, 0);
121  Layout->setSpacing(0);
122  _this->setLayout(Layout);
123  Layout->addWidget(TitleLabel, 1);
124  Layout->addSpacing(Spacing);
125  Layout->addWidget(MaximizeButton);
126  Layout->addWidget(CloseButton);
127  Layout->setAlignment(Qt::AlignCenter);
128 
129  TitleLabel->setVisible(true);
130 }
131 
132 //============================================================================
134  QFrame(parent),
136 {
137  d->FloatingWidget = parent;
138  d->createLayout();
139 
140  auto normalPixmap = this->style()->standardPixmap(QStyle::SP_TitleBarNormalButton, 0, d->MaximizeButton);
141  d->NormalIcon.addPixmap(normalPixmap, QIcon::Normal);
142  d->NormalIcon.addPixmap(internal::createTransparentPixmap(normalPixmap, 0.25), QIcon::Disabled);
143 
144  auto maxPixmap = this->style()->standardPixmap(QStyle::SP_TitleBarMaxButton, 0, d->MaximizeButton);
145  d->MaximizeIcon.addPixmap(maxPixmap, QIcon::Normal);
146  d->MaximizeIcon.addPixmap(internal::createTransparentPixmap(maxPixmap, 0.25), QIcon::Disabled);
148 }
149 
150 //============================================================================
152 {
153  delete d;
154 }
155 
156 //============================================================================
158 {
159  if (ev->button() == Qt::LeftButton)
160  {
162  d->FloatingWidget->startDragging(ev->pos(), d->FloatingWidget->size(),
163  this);
164  return;
165  }
166  Super::mousePressEvent(ev);
167 }
168 
169 
170 //============================================================================
172 {
174  if (d->FloatingWidget)
175  {
177  }
178  Super::mouseReleaseEvent(ev);
179 }
180 
181 
182 //============================================================================
184 {
185  if (!(ev->buttons() & Qt::LeftButton) || DraggingInactive == d->DragState)
186  {
188  Super::mouseMoveEvent(ev);
189  return;
190  }
191 
192  // move floating window
194  {
195  if(d->FloatingWidget->isMaximized())
196  {
197  d->FloatingWidget->showNormal(true);
198  }
200  Super::mouseMoveEvent(ev);
201  return;
202  }
203  Super::mouseMoveEvent(ev);
204 }
205 
206 
207 //============================================================================
209 {
210  d->CloseButton->setEnabled(Enable);
211 }
212 
213 
214 //============================================================================
215 void CFloatingWidgetTitleBar::setTitle(const QString &Text)
216 {
217  d->TitleLabel->setText(Text);
218 }
219 
220 //============================================================================
222 {
224 }
225 
226 
227 //============================================================================
229 {
230  if (event->buttons() & Qt::LeftButton)
231  {
232  emit maximizeRequested();
233  event->accept();
234  }
235  else
236  {
237  QWidget::mouseDoubleClickEvent(event);
238  }
239 }
240 
241 
242 //============================================================================
244 {
245  d->Maximized = maximized;
246  if (maximized)
247  {
248  d->MaximizeButton->setIcon(d->NormalIcon);
249  }
250  else
251  {
252  d->MaximizeButton->setIcon(d->MaximizeIcon);
253  }
254 }
255 
256 
257 //============================================================================
259 {
260  d->MaximizeIcon = Icon;
261  if (d->Maximized)
262  {
264  }
265 }
266 
267 
268 //============================================================================
270 {
271  d->NormalIcon = Icon;
272  if (!d->Maximized)
273  {
275  }
276 }
277 
278 
279 //============================================================================
281 {
282  return d->MaximizeIcon;
283 }
284 
285 
286 //============================================================================
288 {
289  return d->NormalIcon;
290 }
291 
292 
293 } // namespace ads
CFloatingWidgetTitleBar(CFloatingDockContainer *parent=nullptr)
virtual void mouseMoveEvent(QMouseEvent *ev) override
CFloatingWidgetTitleBar * _this
public interface class
void setTitle(const QString &Text)
void setText(const QString &text)
Declaration of CElidingLabel.
MQTTClient d
Definition: test10.c:1656
Private data class of public interface CFloatingWidgetTitleBar.
virtual void finishDragging() override
CElidingLabel tTabLabel
Declaration of CFloatingDockContainer class.
DraggingFloatingWidget.
Definition: ads_globals.h:104
void repolishStyle(QWidget *w, eRepolishChildOptions Options=RepolishIgnoreChildren)
virtual void mousePressEvent(QMouseEvent *ev) override
QToolButton tMaximizeButton
void setElideMode(Qt::TextElideMode mode)
FloatingWidgetTitleBarPrivate * d
private data (pimpl)
Declaration of.
DraggingInactive.
Definition: ads_globals.h:101
void setNormalIcon(const QIcon &Icon)
Declaration of CFloatingWidgetTitleBar class.
QToolButton tCloseButton
static const int Spacing
virtual void mouseDoubleClickEvent(QMouseEvent *event) override
FloatingWidgetTitleBarPrivate(CFloatingWidgetTitleBar *_public)
virtual void mouseReleaseEvent(QMouseEvent *ev) override
void startDragging(const QPoint &DragStartMousePos, const QSize &Size, QWidget *MouseEventHandler)
eDragState
Definition: ads_globals.h:99
QPixmap createTransparentPixmap(const QPixmap &Source, qreal Opacity)
void setMaximizeIcon(const QIcon &Icon)


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:47:34