Update DBus Notify method annotation name
[lxde/liblxqt.git] / configdialog / lxqtpageselectwidget.cpp
CommitLineData
4ef6cee9 1/* BEGIN_COMMON_COPYRIGHT_HEADER
c4af778e 2 * (c)LGPL2+
4ef6cee9 3 *
b9223fe7 4 * LXQt - a lightweight, Qt based, desktop toolset
4ef6cee9 5 * http://razor-qt.org
6 *
7 * Copyright: 2010-2011 Razor team
8 * Authors:
626f1f31 9 * Alexander Sokoloff <sokoloff.a@gmail.com>
4ef6cee9 10 *
11 * This program or library is free software; you can redistribute it
12 * and/or modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
c4af778e 14 * version 2.1 of the License, or (at your option) any later version.
4ef6cee9 15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20
21 * You should have received a copy of the GNU Lesser General
22 * Public License along with this library; if not, write to the
23 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 * Boston, MA 02110-1301 USA
25 *
26 * END_COMMON_COPYRIGHT_HEADER */
27
28
b6483430 29#include "lxqtpageselectwidget.h"
e6389bfc 30#include <QDebug>
afd3b116 31#include <QStyledItemDelegate>
e5a885ac 32#include <QEvent>
154a38ff 33#include <QScrollBar>
b4372f25 34#include <QApplication>
4ef6cee9 35
f05ba5af 36using namespace LXQt;
ad7ee7e8 37
4ef6cee9 38class PageSelectWidgetItemDelegate: public QStyledItemDelegate
39{
40public:
41 explicit PageSelectWidgetItemDelegate(PageSelectWidget *parent = 0);
42 QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
43
44private:
45 PageSelectWidget* mView;
46};
47
48
49/************************************************
50
51 ************************************************/
52PageSelectWidgetItemDelegate::PageSelectWidgetItemDelegate(PageSelectWidget *parent):
53 QStyledItemDelegate(parent),
54 mView(parent)
55{
56}
57
58
59/************************************************
60
61 ************************************************/
62QSize PageSelectWidgetItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
63{
64 QSize size = QStyledItemDelegate::sizeHint(option, index);
e5a885ac
PK
65 //all items should have unified width
66 QStyle * style = option.widget ? option.widget->style() : QApplication::style();
67 //Note: this margin logic follows code in QCommonStylePrivate::viewItemLayout()
68 const int margin = style->pixelMetric(QStyle::PM_FocusFrameHMargin, &option, option.widget) + 1;
69 //considering the icon size too
70 size.setWidth(qMax(mView->maxTextWidth(), option.decorationSize.width()));
71 size.rwidth() += 2 * margin;
4ef6cee9 72 return size;
73}
74
75
76
77/************************************************
78
79 ************************************************/
80PageSelectWidget::PageSelectWidget(QWidget *parent) :
81 QListWidget(parent)
e5a885ac 82 , mMaxTextWidth(0)
4ef6cee9 83{
84 setSelectionRectVisible(false);
85 setViewMode(IconMode);
86 setSpacing(2);
ca9b082d 87 setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
4ef6cee9 88 setWordWrap(true);
89 setDragEnabled(NoDragDrop);
90 setEditTriggers(NoEditTriggers);
91 setTextElideMode(Qt::ElideNone);
e5a885ac 92 setContentsMargins(0, 0, 0, 0);
4ef6cee9 93
94 setItemDelegate(new PageSelectWidgetItemDelegate(this));
e5a885ac
PK
95 connect(model(), &QAbstractItemModel::rowsInserted, this, &PageSelectWidget::updateMaxTextWidth);
96 connect(model(), &QAbstractItemModel::rowsRemoved, this, &PageSelectWidget::updateMaxTextWidth);
97 connect(model(), &QAbstractItemModel::dataChanged, this, &PageSelectWidget::updateMaxTextWidth);
4ef6cee9 98}
99
100
101/************************************************
102
103 ************************************************/
104PageSelectWidget::~PageSelectWidget()
105{
106}
107
e5a885ac
PK
108/************************************************
109
110 ************************************************/
111int PageSelectWidget::maxTextWidth() const
112{
113 return mMaxTextWidth;
114}
115
116/************************************************
117
118 ************************************************/
119QSize PageSelectWidget::viewportSizeHint() const
120{
121 const int spacing2 = spacing() * 2;
122 QSize size{sizeHintForColumn(0) + spacing2, (sizeHintForRow(0) + spacing2) * count()};
123 if (verticalScrollBar()->isVisible())
124 size.rwidth() += verticalScrollBar()->sizeHint().width();
125 return size;
126}
4ef6cee9 127
128/************************************************
129
130 ************************************************/
e5a885ac 131QSize PageSelectWidget::minimumSizeHint() const
4ef6cee9 132{
e5a885ac
PK
133 return QSize{0, 0};
134}
135
136/************************************************
137
138 ************************************************/
139void PageSelectWidget::updateMaxTextWidth()
140{
141 for(int i = count() - 1; 0 <= i; --i)
4ef6cee9 142 {
e5a885ac
PK
143 const QRect r = fontMetrics().boundingRect(QRect{}, Qt::AlignLeft | Qt::TextWordWrap, item(i)->text());
144 mMaxTextWidth = qMax(mMaxTextWidth, r.width());
4ef6cee9 145 }
e5a885ac 146}
4ef6cee9 147
e5a885ac
PK
148/************************************************
149
150 ************************************************/
151bool PageSelectWidget::event(QEvent * event)
152{
153 if (QEvent::StyleChange == event->type())
154 updateMaxTextWidth();
155
156 return QListWidget::event(event);
4ef6cee9 157}