47
47
* A button which always leave room for an icon, even if there is none, so that
48
48
* all button texts are correctly aligned.
50
class SideBarButton : public QToolButton {
50
class SideBarButton : public QToolButton
52
virtual void paintEvent(QPaintEvent* event) {
54
QToolButton::paintEvent(event);
53
virtual void paintEvent(QPaintEvent* event)
56
QToolButton::paintEvent(event);
57
virtual QSize sizeHint() const {
58
const_cast<SideBarButton*>(this)->forceIcon();
59
return QToolButton::sizeHint();
59
virtual QSize sizeHint() const {
60
const_cast<SideBarButton*>(this)->forceIcon();
61
return QToolButton::sizeHint();
64
if (!icon().isNull()) {
68
// Assign an empty icon to the button if there is no icon associated
69
// with the action so that all button texts are correctly aligned.
70
QSize wantedSize = iconSize();
71
if (mEmptyIcon.isNull() || mEmptyIcon.actualSize(wantedSize) != wantedSize) {
72
QPixmap pix(wantedSize);
73
pix.fill(Qt::transparent);
74
mEmptyIcon.addPixmap(pix);
67
if (!icon().isNull()) {
71
// Assign an empty icon to the button if there is no icon associated
72
// with the action so that all button texts are correctly aligned.
73
QSize wantedSize = iconSize();
74
if (mEmptyIcon.isNull() || mEmptyIcon.actualSize(wantedSize) != wantedSize) {
75
QPixmap pix(wantedSize);
76
pix.fill(Qt::transparent);
77
mEmptyIcon.addPixmap(pix);
83
85
//- SideBarGroup ---------------------------------------------------------------
84
86
struct SideBarGroupPrivate {
90
91
SideBarGroup::SideBarGroup(const QString& title)
92
, d(new SideBarGroupPrivate) {
94
d->mTitleLabel = new QLabel(this);
95
d->mTitleLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
96
d->mTitleLabel->setFixedHeight(d->mTitleLabel->sizeHint().height() * 3 / 2);
97
QFont font(d->mTitleLabel->font());
99
d->mTitleLabel->setFont(font);
100
d->mTitleLabel->setText(title);
102
QVBoxLayout* layout = new QVBoxLayout(this);
103
layout->setMargin(0);
104
layout->setSpacing(0);
106
layout->addWidget(d->mTitleLabel);
112
SideBarGroup::~SideBarGroup() {
117
void SideBarGroup::paintEvent(QPaintEvent* event) {
118
QFrame::paintEvent(event);
119
if (parentWidget()->layout()->indexOf(this) != 0) {
120
// Draw a separator, but only if we are not the first group
121
QPainter painter(this);
122
QPen pen(palette().mid().color());
124
painter.drawLine(rect().topLeft(), rect().topRight());
129
void SideBarGroup::addWidget(QWidget* widget) {
130
widget->setParent(d->mContainer);
131
d->mContainer->layout()->addWidget(widget);
135
void SideBarGroup::clear() {
137
d->mContainer->deleteLater();
140
d->mContainer = new QFrame(this);
141
QVBoxLayout* containerLayout = new QVBoxLayout(d->mContainer);
142
containerLayout->setMargin(0);
143
containerLayout->setSpacing(0);
145
layout()->addWidget(d->mContainer);
149
void SideBarGroup::addAction(QAction* action) {
150
int size = KIconLoader::global()->currentSize(KIconLoader::Small);
151
QToolButton* button = new SideBarButton();
152
button->setFocusPolicy(Qt::NoFocus);
153
button->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
154
button->setAutoRaise(true);
155
button->setDefaultAction(action);
156
button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
157
button->setIconSize(QSize(size, size));
158
if (action->menu()) {
159
button->setPopupMode(QToolButton::InstantPopup);
93
, d(new SideBarGroupPrivate)
96
d->mTitleLabel = new QLabel(this);
97
d->mTitleLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
98
d->mTitleLabel->setFixedHeight(d->mTitleLabel->sizeHint().height() * 3 / 2);
99
QFont font(d->mTitleLabel->font());
101
d->mTitleLabel->setFont(font);
102
d->mTitleLabel->setText(title);
104
QVBoxLayout* layout = new QVBoxLayout(this);
105
layout->setMargin(0);
106
layout->setSpacing(0);
108
layout->addWidget(d->mTitleLabel);
113
SideBarGroup::~SideBarGroup()
118
void SideBarGroup::paintEvent(QPaintEvent* event)
120
QFrame::paintEvent(event);
121
if (parentWidget()->layout()->indexOf(this) != 0) {
122
// Draw a separator, but only if we are not the first group
123
QPainter painter(this);
124
QPen pen(palette().mid().color());
126
painter.drawLine(rect().topLeft(), rect().topRight());
130
void SideBarGroup::addWidget(QWidget* widget)
132
widget->setParent(d->mContainer);
133
d->mContainer->layout()->addWidget(widget);
136
void SideBarGroup::clear()
139
d->mContainer->deleteLater();
142
d->mContainer = new QFrame(this);
143
QVBoxLayout* containerLayout = new QVBoxLayout(d->mContainer);
144
containerLayout->setMargin(0);
145
containerLayout->setSpacing(0);
147
layout()->addWidget(d->mContainer);
150
void SideBarGroup::addAction(QAction* action)
152
int size = KIconLoader::global()->currentSize(KIconLoader::Small);
153
QToolButton* button = new SideBarButton();
154
button->setFocusPolicy(Qt::NoFocus);
155
button->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
156
button->setAutoRaise(true);
157
button->setDefaultAction(action);
158
button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
159
button->setIconSize(QSize(size, size));
160
if (action->menu()) {
161
button->setPopupMode(QToolButton::InstantPopup);
165
166
//- SideBarPage ----------------------------------------------------------------
166
167
struct SideBarPagePrivate {
168
QVBoxLayout* mLayout;
169
QVBoxLayout* mLayout;
172
172
SideBarPage::SideBarPage(const QString& title)
174
174
, d(new SideBarPagePrivate)
178
d->mLayout = new QVBoxLayout(this);
179
d->mLayout->setMargin(0);
183
SideBarPage::~SideBarPage() {
188
const QString& SideBarPage::title() const {
193
void SideBarPage::addWidget(QWidget* widget) {
194
d->mLayout->addWidget(widget);
198
void SideBarPage::addStretch() {
199
d->mLayout->addStretch();
178
d->mLayout = new QVBoxLayout(this);
179
d->mLayout->setMargin(0);
182
SideBarPage::~SideBarPage()
187
const QString& SideBarPage::title() const
192
void SideBarPage::addWidget(QWidget* widget)
194
d->mLayout->addWidget(widget);
197
void SideBarPage::addStretch()
199
d->mLayout->addStretch();
203
202
//- SideBar --------------------------------------------------------------------
204
203
struct SideBarPrivate {
208
206
SideBar::SideBar(QWidget* parent)
209
207
: QTabWidget(parent)
210
, d(new SideBarPrivate) {
211
setFont(KGlobalSettings::smallestReadableFont());
212
tabBar()->setDocumentMode(true);
213
tabBar()->setUsesScrollButtons(false);
214
tabBar()->setFocusPolicy(Qt::NoFocus);
215
setTabPosition(QTabWidget::South);
216
setElideMode(Qt::ElideRight);
220
SideBar::~SideBar() {
225
QSize SideBar::sizeHint() const {
226
return QSize(200, 200);
230
void SideBar::addPage(SideBarPage* page) {
231
addTab(page, page->title());
235
QString SideBar::currentPage() const {
236
return currentWidget()->objectName();
240
void SideBar::setCurrentPage(const QString& name) {
241
for (int index = 0; index < count(); ++index) {
242
if (widget(index)->objectName() == name) {
243
setCurrentIndex(index);
208
, d(new SideBarPrivate)
210
setFont(KGlobalSettings::smallestReadableFont());
211
tabBar()->setDocumentMode(true);
212
tabBar()->setUsesScrollButtons(false);
213
tabBar()->setFocusPolicy(Qt::NoFocus);
214
setTabPosition(QTabWidget::South);
215
setElideMode(Qt::ElideRight);
223
QSize SideBar::sizeHint() const
225
return QSize(200, 200);
228
void SideBar::addPage(SideBarPage* page)
230
addTab(page, page->title());
233
QString SideBar::currentPage() const
235
return currentWidget()->objectName();
238
void SideBar::setCurrentPage(const QString& name)
240
for (int index = 0; index < count(); ++index) {
241
if (widget(index)->objectName() == name) {
242
setCurrentIndex(index);