|
| 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 | + |
| 20 | +//============================================================================ |
| 21 | +/// \file FloatingWidgetTitleBar.cpp |
| 22 | +/// \author Uwe Kindler |
| 23 | +/// \date 13.05.2019 |
| 24 | +/// \brief Implementation of CFloatingWidgetTitleBar class |
| 25 | +//============================================================================ |
| 26 | + |
| 27 | +//============================================================================ |
| 28 | +// INCLUDES |
| 29 | +//============================================================================ |
| 30 | +#include "FloatingWidgetTitleBar.h" |
| 31 | + |
| 32 | +#include <iostream> |
| 33 | + |
| 34 | +#include <QHBoxLayout> |
| 35 | +#include <QPushButton> |
| 36 | +#include <QToolButton> |
| 37 | +#include <QPixmap> |
| 38 | +#include <QStyle> |
| 39 | +#include <QMouseEvent> |
| 40 | + |
| 41 | +#include "ads_globals.h" |
| 42 | +#include "ElidingLabel.h" |
| 43 | +#include "FloatingDockContainer.h" |
| 44 | + |
| 45 | +namespace ads |
| 46 | +{ |
| 47 | + |
| 48 | +using tTabLabel = CElidingLabel; |
| 49 | +using tCloseButton = QPushButton; |
| 50 | + |
| 51 | + |
| 52 | +/** |
| 53 | + * @brief Private data class of public interface CFloatingWidgetTitleBar |
| 54 | + */ |
| 55 | +struct FloatingWidgetTitleBarPrivate |
| 56 | +{ |
| 57 | + CFloatingWidgetTitleBar* _this; ///< public interface class |
| 58 | + QLabel* IconLabel = nullptr; |
| 59 | + tTabLabel* TitleLabel; |
| 60 | + tCloseButton* CloseButton = nullptr; |
| 61 | + CFloatingDockContainer* FloatingWidget = nullptr; |
| 62 | + eDragState DragState = DraggingInactive; |
| 63 | + |
| 64 | + FloatingWidgetTitleBarPrivate(CFloatingWidgetTitleBar* _public) : _this(_public) {} |
| 65 | + |
| 66 | + /** |
| 67 | + * Creates the complete layout including all controls |
| 68 | + */ |
| 69 | + void createLayout(); |
| 70 | +}; |
| 71 | + |
| 72 | + |
| 73 | +//============================================================================ |
| 74 | +void FloatingWidgetTitleBarPrivate::createLayout() |
| 75 | +{ |
| 76 | + TitleLabel = new tTabLabel(); |
| 77 | + TitleLabel->setElideMode(Qt::ElideRight); |
| 78 | + TitleLabel->setText("DockWidget->windowTitle()"); |
| 79 | + TitleLabel->setObjectName("floatingTitleLabel"); |
| 80 | + TitleLabel->setAlignment(Qt::AlignLeft); |
| 81 | + |
| 82 | + CloseButton = new tCloseButton(); |
| 83 | + CloseButton->setObjectName("floatingTitleCloseButton"); |
| 84 | + CloseButton->setFlat(true); |
| 85 | + //CloseButton->setAutoRaise(true); |
| 86 | + // The standard icons do does not look good on high DPI screens |
| 87 | + QIcon CloseIcon; |
| 88 | + QPixmap normalPixmap = _this->style()->standardPixmap(QStyle::SP_TitleBarCloseButton, 0, CloseButton); |
| 89 | + CloseIcon.addPixmap(normalPixmap, QIcon::Normal); |
| 90 | + CloseIcon.addPixmap(internal::createTransparentPixmap(normalPixmap, 0.25), QIcon::Disabled); |
| 91 | + CloseButton->setIcon(_this->style()->standardIcon(QStyle::SP_TitleBarCloseButton)); |
| 92 | + CloseButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
| 93 | + CloseButton->setVisible(true); |
| 94 | + CloseButton->setFocusPolicy(Qt::NoFocus); |
| 95 | + _this->connect(CloseButton, SIGNAL(clicked()), SIGNAL(closeRequested())); |
| 96 | + |
| 97 | + QFontMetrics fm(TitleLabel->font()); |
| 98 | + int Spacing = qRound(fm.height() / 4.0); |
| 99 | + |
| 100 | + // Fill the layout |
| 101 | + QBoxLayout* Layout = new QBoxLayout(QBoxLayout::LeftToRight); |
| 102 | + Layout->setContentsMargins(6,0,0,0); |
| 103 | + Layout->setSpacing(0); |
| 104 | + _this->setLayout(Layout); |
| 105 | + Layout->addWidget(TitleLabel, 1); |
| 106 | + Layout->addSpacing(Spacing); |
| 107 | + Layout->addWidget(CloseButton); |
| 108 | + Layout->setAlignment(Qt::AlignCenter); |
| 109 | + |
| 110 | + TitleLabel->setVisible(true); |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | +//============================================================================ |
| 115 | +CFloatingWidgetTitleBar::CFloatingWidgetTitleBar(CFloatingDockContainer *parent) |
| 116 | + : QWidget(parent), |
| 117 | + d(new FloatingWidgetTitleBarPrivate(this)) |
| 118 | +{ |
| 119 | + d->FloatingWidget = parent; |
| 120 | + d->createLayout(); |
| 121 | +} |
| 122 | + |
| 123 | + |
| 124 | +//============================================================================ |
| 125 | +CFloatingWidgetTitleBar::~CFloatingWidgetTitleBar() |
| 126 | +{ |
| 127 | + delete d; |
| 128 | +} |
| 129 | + |
| 130 | + |
| 131 | +//============================================================================ |
| 132 | +void CFloatingWidgetTitleBar::mousePressEvent(QMouseEvent* ev) |
| 133 | +{ |
| 134 | + if (ev->button() == Qt::LeftButton) |
| 135 | + { |
| 136 | + d->DragState = DraggingFloatingWidget; |
| 137 | + d->FloatingWidget->startDragging(ev->pos(), d->FloatingWidget->size(), this); |
| 138 | + return; |
| 139 | + } |
| 140 | + Super::mousePressEvent(ev); |
| 141 | +} |
| 142 | + |
| 143 | + |
| 144 | +//============================================================================ |
| 145 | +void CFloatingWidgetTitleBar::mouseReleaseEvent(QMouseEvent* ev) |
| 146 | +{ |
| 147 | + d->DragState = DraggingInactive; |
| 148 | + Super::mouseReleaseEvent(ev); |
| 149 | +} |
| 150 | + |
| 151 | + |
| 152 | +//============================================================================ |
| 153 | +void CFloatingWidgetTitleBar::mouseMoveEvent(QMouseEvent* ev) |
| 154 | +{ |
| 155 | + if (!(ev->buttons() & Qt::LeftButton) || DraggingInactive == d->DragState) |
| 156 | + { |
| 157 | + d->DragState = DraggingInactive; |
| 158 | + Super::mouseMoveEvent(ev); |
| 159 | + return; |
| 160 | + } |
| 161 | + |
| 162 | + // move floating window |
| 163 | + if (DraggingFloatingWidget == d->DragState) |
| 164 | + { |
| 165 | + d->FloatingWidget->moveFloating(); |
| 166 | + Super::mouseMoveEvent(ev); |
| 167 | + return; |
| 168 | + } |
| 169 | + Super::mouseMoveEvent(ev); |
| 170 | +} |
| 171 | + |
| 172 | + |
| 173 | +//============================================================================ |
| 174 | +void CFloatingWidgetTitleBar::enableCloseButton(bool Enable) |
| 175 | +{ |
| 176 | + d->CloseButton->setEnabled(Enable); |
| 177 | +} |
| 178 | + |
| 179 | + |
| 180 | +//============================================================================ |
| 181 | +void CFloatingWidgetTitleBar::setTitle(const QString& Text) |
| 182 | +{ |
| 183 | + d->TitleLabel->setText(Text); |
| 184 | +} |
| 185 | + |
| 186 | + |
| 187 | +} // namespace ads |
0 commit comments