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
ads_globals.h
Declaration of.
ads::CFloatingWidgetTitleBar::mousePressEvent
virtual void mousePressEvent(QMouseEvent *ev) override
Definition: FloatingWidgetTitleBar.cpp:157
ads::CFloatingDockContainer::finishDragging
virtual void finishDragging() override
Definition: FloatingDockContainer.cpp:1035
ads::DraggingFloatingWidget
@ DraggingFloatingWidget
DraggingFloatingWidget.
Definition: ads_globals.h:104
FloatingWidgetTitleBar.h
Declaration of CFloatingWidgetTitleBar class.
ads::FloatingWidgetTitleBarPrivate
Private data class of public interface CFloatingWidgetTitleBar.
Definition: FloatingWidgetTitleBar.cpp:54
ads::FloatingWidgetTitleBarPrivate::TitleLabel
tTabLabel * TitleLabel
Definition: FloatingWidgetTitleBar.cpp:58
ads::CFloatingWidgetTitleBar::setNormalIcon
void setNormalIcon(const QIcon &Icon)
Definition: FloatingWidgetTitleBar.cpp:269
FloatingDockContainer.h
Declaration of CFloatingDockContainer class.
ads::CElidingLabel::setElideMode
void setElideMode(Qt::TextElideMode mode)
Definition: ElidingLabel.cpp:117
ads::CFloatingWidgetTitleBar::updateStyle
void updateStyle()
Definition: FloatingWidgetTitleBar.cpp:221
ads::FloatingWidgetTitleBarPrivate::FloatingWidgetTitleBarPrivate
FloatingWidgetTitleBarPrivate(CFloatingWidgetTitleBar *_public)
Definition: FloatingWidgetTitleBar.cpp:67
ads::tCloseButton
QToolButton tCloseButton
Definition: FloatingWidgetTitleBar.cpp:48
ads::CFloatingWidgetTitleBar::mouseDoubleClickEvent
virtual void mouseDoubleClickEvent(QMouseEvent *event) override
Definition: FloatingWidgetTitleBar.cpp:228
ads::tMaximizeButton
QToolButton tMaximizeButton
Definition: FloatingWidgetTitleBar.cpp:49
ads::FloatingWidgetTitleBarPrivate::MaximizeIcon
QIcon MaximizeIcon
Definition: FloatingWidgetTitleBar.cpp:63
ads::CFloatingWidgetTitleBar::normalIcon
QIcon normalIcon
Definition: FloatingWidgetTitleBar.h:53
ads::CFloatingWidgetTitleBar::setTitle
void setTitle(const QString &Text)
Definition: FloatingWidgetTitleBar.cpp:215
ads::FloatingWidgetTitleBarPrivate::Maximized
bool Maximized
Definition: FloatingWidgetTitleBar.cpp:65
ads::CFloatingWidgetTitleBar::CFloatingWidgetTitleBar
CFloatingWidgetTitleBar(CFloatingDockContainer *parent=nullptr)
Definition: FloatingWidgetTitleBar.cpp:133
ads::FloatingWidgetTitleBarPrivate::FloatingWidget
CFloatingDockContainer * FloatingWidget
Definition: FloatingWidgetTitleBar.cpp:61
ads::FloatingWidgetTitleBarPrivate::DragState
eDragState DragState
Definition: FloatingWidgetTitleBar.cpp:62
ads::CFloatingWidgetTitleBar::mouseReleaseEvent
virtual void mouseReleaseEvent(QMouseEvent *ev) override
Definition: FloatingWidgetTitleBar.cpp:171
ads
Definition: ads_globals.h:69
ads::CFloatingDockContainer::moveFloating
void moveFloating() override
Definition: FloatingDockContainer.cpp:901
ads::CFloatingWidgetTitleBar::setMaximizeIcon
void setMaximizeIcon(const QIcon &Icon)
Definition: FloatingWidgetTitleBar.cpp:258
ads::CFloatingWidgetTitleBar::mouseMoveEvent
virtual void mouseMoveEvent(QMouseEvent *ev) override
Definition: FloatingWidgetTitleBar.cpp:183
ads::FloatingWidgetTitleBarPrivate::NormalIcon
QIcon NormalIcon
Definition: FloatingWidgetTitleBar.cpp:64
ads::CFloatingWidgetTitleBar
Definition: FloatingWidgetTitleBar.h:49
ads::FloatingWidgetTitleBarPrivate::createLayout
void createLayout()
Definition: FloatingWidgetTitleBar.cpp:79
ads::CElidingLabel::setText
void setText(const QString &text)
Definition: ElidingLabel.cpp:198
d
d
ads::CFloatingWidgetTitleBar::maximizeIcon
QIcon maximizeIcon
Definition: FloatingWidgetTitleBar.h:52
ads::eDragState
eDragState
Definition: ads_globals.h:99
ads::CFloatingWidgetTitleBar::enableCloseButton
void enableCloseButton(bool Enable)
Definition: FloatingWidgetTitleBar.cpp:208
ads::FloatingWidgetTitleBarPrivate::CloseButton
tCloseButton * CloseButton
Definition: FloatingWidgetTitleBar.cpp:59
Spacing
static const int Spacing
Definition: qwt_arrow_button.cpp:20
ads::CFloatingWidgetTitleBar::d
FloatingWidgetTitleBarPrivate * d
private data (pimpl)
Definition: FloatingWidgetTitleBar.h:55
ads::FloatingWidgetTitleBarPrivate::IconLabel
QLabel * IconLabel
Definition: FloatingWidgetTitleBar.cpp:57
ads::internal::RepolishDirectChildren
@ RepolishDirectChildren
Definition: ads_globals.h:283
ads::CFloatingDockContainer
Definition: FloatingDockContainer.h:104
ads::CFloatingWidgetTitleBar::setMaximizedIcon
void setMaximizedIcon(bool maximized)
Definition: FloatingWidgetTitleBar.cpp:243
ads::tTabLabel
CElidingLabel tTabLabel
Definition: DockWidgetTab.cpp:58
ads::CFloatingWidgetTitleBar::maximizeRequested
void maximizeRequested()
ads::CElidingLabel
Definition: ElidingLabel.h:46
ElidingLabel.h
Declaration of CElidingLabel.
ads::DraggingInactive
@ DraggingInactive
DraggingInactive.
Definition: ads_globals.h:101
ads::CFloatingDockContainer::startDragging
void startDragging(const QPoint &DragStartMousePos, const QSize &Size, QWidget *MouseEventHandler)
Definition: FloatingDockContainer.h:137
ads::CFloatingWidgetTitleBar::~CFloatingWidgetTitleBar
virtual ~CFloatingWidgetTitleBar()
Definition: FloatingWidgetTitleBar.cpp:151
ads::internal::repolishStyle
void repolishStyle(QWidget *w, eRepolishChildOptions Options=RepolishIgnoreChildren)
Definition: ads_globals.cpp:367
ads::FloatingWidgetTitleBarPrivate::_this
CFloatingWidgetTitleBar * _this
public interface class
Definition: FloatingWidgetTitleBar.cpp:56
ads::internal::createTransparentPixmap
QPixmap createTransparentPixmap(const QPixmap &Source, qreal Opacity)
Definition: ads_globals.cpp:316
ads::FloatingWidgetTitleBarPrivate::MaximizeButton
tMaximizeButton * MaximizeButton
Definition: FloatingWidgetTitleBar.cpp:60


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:44