Skip to content

Commit a4838a4

Browse files
author
Uwe Kindler
committed
Added ElidingLabel to support text eliding for dock area tabs, removed debug output
1 parent 74b9d35 commit a4838a4

10 files changed

+348
-25
lines changed

src/DockAreaTabBar.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
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+
120
//============================================================================
221
/// \file DockAreaTabBar.cpp
322
/// \author Uwe Kindler
@@ -24,7 +43,6 @@
2443
#include "DockWidget.h"
2544
#include "DockWidgetTab.h"
2645

27-
#include <iostream>
2846

2947
namespace ads
3048
{
@@ -186,8 +204,6 @@ void CDockAreaTabBar::mouseMoveEvent(QMouseEvent* ev)
186204
return;
187205
}
188206

189-
/*int DragDistanceY = qAbs(d->DragStartMousePos.y() - ev->pos().y());
190-
int MinDragDistanceY = this->height() / 2;*/
191207
int DragDistance = (d->DragStartMousePos - ev->pos()).manhattanLength();
192208
if (DragDistance >= CDockManager::startDragDistance())
193209
{

src/DockAreaTitleBar.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
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+
120
//============================================================================
221
/// \file DockAreaTitleBar.cpp
322
/// \author Uwe Kindler
@@ -29,7 +48,6 @@
2948
#include "DockWidgetTab.h"
3049
#include "DockAreaTabBar.h"
3150

32-
#include <iostream>
3351

3452
namespace ads
3553
{
@@ -226,7 +244,6 @@ void CDockAreaTitleBar::onCloseButtonClicked()
226244
//============================================================================
227245
void CDockAreaTitleBar::onUndockButtonClicked()
228246
{
229-
std::cout << "CDockAreaTitleBar::onUndockButtonClicked" << std::endl;
230247
d->TabBar->makeAreaFloating(mapFromGlobal(QCursor::pos()));
231248
}
232249

src/DockAreaWidget.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@
5454
#include "DockSplitter.h"
5555
#include "DockAreaTitleBar.h"
5656

57-
#include <iostream>
58-
5957

6058
namespace ads
6159
{
@@ -392,7 +390,6 @@ void CDockAreaWidget::insertDockWidget(int index, CDockWidget* DockWidget,
392390
void CDockAreaWidget::removeDockWidget(CDockWidget* DockWidget)
393391
{
394392
qDebug() << "CDockAreaWidget::removeDockWidget";
395-
std::cout << "CDockAreaWidget::removeDockWidget" << std::endl;
396393
auto NextOpenDockWidget = nextOpenDockWidget(DockWidget);
397394

398395
d->ContentsLayout->removeWidget(DockWidget);
@@ -434,7 +431,6 @@ void CDockAreaWidget::removeDockWidget(CDockWidget* DockWidget)
434431
//============================================================================
435432
void CDockAreaWidget::hideAreaWithNoVisibleContent()
436433
{
437-
std::cout << "CDockAreaWidget::hideAreaWithNoVisibleContent()" << std::endl;
438434
this->toggleView(false);
439435

440436
// Hide empty parent splitter

src/DockContainerWidget.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
#include "DockSplitter.h"
5050

5151
#include <QElapsedTimer>
52-
#include <iostream>
5352

5453

5554
namespace ads

src/DockWidgetTab.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
//============================================================================
2929
// INCLUDES
3030
//============================================================================
31+
#include <ElidingLabel.h>
3132
#include "DockWidgetTab.h"
3233

3334
#include <QBoxLayout>
@@ -45,8 +46,6 @@
4546
#include "DockOverlay.h"
4647
#include "DockManager.h"
4748

48-
#include <iostream>
49-
5049
namespace ads
5150
{
5251
/**
@@ -60,6 +59,7 @@ enum eDragState
6059
DraggingFloatingWidget//!< DraggingFloatingWidget
6160
};
6261

62+
using tTabLabel = CElidingLabel;
6363

6464
/**
6565
* Private data class of CDockWidgetTab class (pimpl)
@@ -69,7 +69,7 @@ struct DockWidgetTabPrivate
6969
CDockWidgetTab* _this;
7070
CDockWidget* DockWidget;
7171
QLabel* IconLabel;
72-
QLabel* TitleLabel;
72+
tTabLabel* TitleLabel;
7373
QPoint DragStartMousePosition;
7474
bool IsActiveTab = false;
7575
CDockAreaWidget* DockArea = nullptr;
@@ -137,15 +137,17 @@ void DockWidgetTabPrivate::createLayout()
137137

138138
IconLabel = new QLabel();
139139
IconLabel->setAlignment(Qt::AlignVCenter);
140+
IconLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
140141
l->addWidget(IconLabel, Qt::AlignVCenter);
141142

142-
TitleLabel = new QLabel();
143+
TitleLabel = new tTabLabel();
144+
TitleLabel->setElideMode(Qt::ElideRight);
145+
TitleLabel->setText(DockWidget->windowTitle());
143146
TitleLabel->setObjectName("dockWidgetTabLabel");
144147
l->addWidget(TitleLabel, 1);
145148

146149
IconLabel->setVisible(false);
147150
TitleLabel->setVisible(true);
148-
TitleLabel->setText(DockWidget->windowTitle());
149151
}
150152

151153
//============================================================================

src/ElidingLabel.cpp

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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 ElidingLabel.cpp
22+
/// \author Uwe Kindler
23+
/// \date 05.11.2018
24+
/// \brief Implementation of CElidingLabel
25+
//============================================================================
26+
27+
//============================================================================
28+
// INCLUDES
29+
//============================================================================
30+
#include <ElidingLabel.h>
31+
#include <QMouseEvent>
32+
33+
34+
namespace ads
35+
{
36+
/**
37+
* Private data of public CClickableLabel
38+
*/
39+
struct ElidingLabelPrivate
40+
{
41+
CElidingLabel* _this;
42+
Qt::TextElideMode ElideMode = Qt::ElideNone;
43+
QString Text;
44+
45+
ElidingLabelPrivate(CElidingLabel* _public) : _this(_public) {}
46+
47+
void elideText(int Width);
48+
49+
/**
50+
* Convenience function to check if the
51+
*/
52+
bool isModeElideNone() const
53+
{
54+
return Qt::ElideNone == ElideMode;
55+
}
56+
};
57+
58+
59+
//============================================================================
60+
void ElidingLabelPrivate::elideText(int Width)
61+
{
62+
if (isModeElideNone())
63+
{
64+
return;
65+
}
66+
QFontMetrics fm = _this->fontMetrics();
67+
QString str = fm.elidedText(Text, ElideMode, Width - _this->margin() * 2 - _this->indent());
68+
if (str == "")
69+
{
70+
str = Text.at(0);
71+
}
72+
_this->QLabel::setText(str);
73+
}
74+
75+
76+
//============================================================================
77+
CElidingLabel::CElidingLabel(QWidget* parent, Qt::WindowFlags f)
78+
: QLabel(parent, f),
79+
d(new ElidingLabelPrivate(this))
80+
{
81+
82+
}
83+
84+
85+
//============================================================================
86+
CElidingLabel::CElidingLabel(const QString& text, QWidget* parent, Qt::WindowFlags f)
87+
: QLabel(text, parent,f),
88+
d(new ElidingLabelPrivate(this))
89+
{
90+
d->Text = text;
91+
setToolTip(text);
92+
}
93+
94+
95+
//============================================================================
96+
CElidingLabel::~CElidingLabel()
97+
{
98+
delete d;
99+
}
100+
101+
102+
//============================================================================
103+
Qt::TextElideMode CElidingLabel::elideMode() const
104+
{
105+
return d->ElideMode;
106+
}
107+
108+
109+
//============================================================================
110+
void CElidingLabel::setElideMode(Qt::TextElideMode mode)
111+
{
112+
d->ElideMode = mode;
113+
d->elideText(size().width());
114+
}
115+
116+
117+
//============================================================================
118+
void CElidingLabel::mouseReleaseEvent(QMouseEvent* event)
119+
{
120+
if (event->button() != Qt::LeftButton)
121+
{
122+
return;
123+
}
124+
125+
emit clicked();
126+
}
127+
128+
129+
//============================================================================
130+
void CElidingLabel::mouseDoubleClickEvent( QMouseEvent *ev )
131+
{
132+
Q_UNUSED(ev)
133+
emit doubleClicked();
134+
}
135+
136+
137+
//============================================================================
138+
void CElidingLabel::resizeEvent(QResizeEvent *event)
139+
{
140+
if (!d->isModeElideNone())
141+
{
142+
d->elideText(event->size().width());
143+
}
144+
QLabel::resizeEvent(event);
145+
}
146+
147+
148+
//============================================================================
149+
QSize CElidingLabel::minimumSizeHint() const
150+
{
151+
if (pixmap() != nullptr || d->isModeElideNone())
152+
{
153+
return QLabel::minimumSizeHint();
154+
}
155+
const QFontMetrics &fm = fontMetrics();
156+
QSize size(fm.width(""), fm.height());
157+
return size;
158+
}
159+
160+
161+
//============================================================================
162+
QSize CElidingLabel::sizeHint() const
163+
{
164+
if (pixmap() != nullptr || d->isModeElideNone())
165+
{
166+
return QLabel::sizeHint();
167+
}
168+
const QFontMetrics& fm = fontMetrics();
169+
QSize size(fm.width(d->Text), QLabel::sizeHint().height());
170+
return size;
171+
}
172+
173+
174+
//============================================================================
175+
void CElidingLabel::setText(const QString &text)
176+
{
177+
if (d->isModeElideNone())
178+
{
179+
Super::setText(text);
180+
}
181+
else
182+
{
183+
d->Text = text;
184+
setToolTip( text );
185+
d->elideText(this->size().width());
186+
}
187+
}
188+
189+
190+
//============================================================================
191+
QString CElidingLabel::text() const
192+
{
193+
return d->Text;
194+
}
195+
} // namespace QtLabb
196+
197+
//---------------------------------------------------------------------------
198+
// EOF ClickableLabel.cpp

0 commit comments

Comments
 (0)