Update DBus Notify method annotation name
[lxde/liblxqt.git] / configdialog / lxqtpageselectwidget.cpp
1 /* BEGIN_COMMON_COPYRIGHT_HEADER
2 * (c)LGPL2+
3 *
4 * LXQt - a lightweight, Qt based, desktop toolset
5 * http://razor-qt.org
6 *
7 * Copyright: 2010-2011 Razor team
8 * Authors:
9 * Alexander Sokoloff <sokoloff.a@gmail.com>
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
14 * version 2.1 of the License, or (at your option) any later version.
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
29 #include "lxqtpageselectwidget.h"
30 #include <QDebug>
31 #include <QStyledItemDelegate>
32 #include <QEvent>
33 #include <QScrollBar>
34 #include <QApplication>
35
36 using namespace LXQt;
37
38 class PageSelectWidgetItemDelegate: public QStyledItemDelegate
39 {
40 public:
41 explicit PageSelectWidgetItemDelegate(PageSelectWidget *parent = 0);
42 QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
43
44 private:
45 PageSelectWidget* mView;
46 };
47
48
49 /************************************************
50
51 ************************************************/
52 PageSelectWidgetItemDelegate::PageSelectWidgetItemDelegate(PageSelectWidget *parent):
53 QStyledItemDelegate(parent),
54 mView(parent)
55 {
56 }
57
58
59 /************************************************
60
61 ************************************************/
62 QSize PageSelectWidgetItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
63 {
64 QSize size = QStyledItemDelegate::sizeHint(option, index);
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;
72 return size;
73 }
74
75
76
77 /************************************************
78
79 ************************************************/
80 PageSelectWidget::PageSelectWidget(QWidget *parent) :
81 QListWidget(parent)
82 , mMaxTextWidth(0)
83 {
84 setSelectionRectVisible(false);
85 setViewMode(IconMode);
86 setSpacing(2);
87 setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
88 setWordWrap(true);
89 setDragEnabled(NoDragDrop);
90 setEditTriggers(NoEditTriggers);
91 setTextElideMode(Qt::ElideNone);
92 setContentsMargins(0, 0, 0, 0);
93
94 setItemDelegate(new PageSelectWidgetItemDelegate(this));
95 connect(model(), &QAbstractItemModel::rowsInserted, this, &PageSelectWidget::updateMaxTextWidth);
96 connect(model(), &QAbstractItemModel::rowsRemoved, this, &PageSelectWidget::updateMaxTextWidth);
97 connect(model(), &QAbstractItemModel::dataChanged, this, &PageSelectWidget::updateMaxTextWidth);
98 }
99
100
101 /************************************************
102
103 ************************************************/
104 PageSelectWidget::~PageSelectWidget()
105 {
106 }
107
108 /************************************************
109
110 ************************************************/
111 int PageSelectWidget::maxTextWidth() const
112 {
113 return mMaxTextWidth;
114 }
115
116 /************************************************
117
118 ************************************************/
119 QSize 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 }
127
128 /************************************************
129
130 ************************************************/
131 QSize PageSelectWidget::minimumSizeHint() const
132 {
133 return QSize{0, 0};
134 }
135
136 /************************************************
137
138 ************************************************/
139 void PageSelectWidget::updateMaxTextWidth()
140 {
141 for(int i = count() - 1; 0 <= i; --i)
142 {
143 const QRect r = fontMetrics().boundingRect(QRect{}, Qt::AlignLeft | Qt::TextWordWrap, item(i)->text());
144 mMaxTextWidth = qMax(mMaxTextWidth, r.width());
145 }
146 }
147
148 /************************************************
149
150 ************************************************/
151 bool PageSelectWidget::event(QEvent * event)
152 {
153 if (QEvent::StyleChange == event->type())
154 updateMaxTextWidth();
155
156 return QListWidget::event(event);
157 }