1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
/* BEGIN_COMMON_COPYRIGHT_HEADER
* (c)LGPL2+
*
* LXQt - a lightweight, Qt based, desktop toolset
* https://lxqt.org
*
* Copyright: 2010-2011 Razor team
* Authors:
* Alexander Sokoloff <sokoloff.a@gmail.com>
*
* This program or library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* END_COMMON_COPYRIGHT_HEADER */
#include "lxqtpageselectwidget.h"
#include <QDebug>
#include <QStyledItemDelegate>
#include <QEvent>
#include <QScrollBar>
#include <QApplication>
using namespace LXQt;
class PageSelectWidgetItemDelegate: public QStyledItemDelegate
{
public:
explicit PageSelectWidgetItemDelegate(PageSelectWidget *parent = nullptr);
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
private:
PageSelectWidget* mView;
};
/************************************************
************************************************/
PageSelectWidgetItemDelegate::PageSelectWidgetItemDelegate(PageSelectWidget *parent):
QStyledItemDelegate(parent),
mView(parent)
{
}
/************************************************
************************************************/
QSize PageSelectWidgetItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QVariant value = index.data(Qt::SizeHintRole);
if (value.isValid())
return qvariant_cast<QSize>(value);
//all items should have unified width
QStyleOptionViewItem opt = option;
initStyleOption(&opt, index);
const QWidget *widget = option.widget;
QStyle *style = widget ? widget->style() : QApplication::style();
QSize size = style->sizeFromContents(QStyle::CT_ItemViewItem, &opt, QSize(), widget);
//NOTE: this margin logic follows code in QCommonStylePrivate::viewItemLayout()
const int margin = style->pixelMetric(QStyle::PM_FocusFrameHMargin, &option, option.widget) + 1;
// Find the extra vertical gap by subtracting the height of the text when wrapped within
// the default size from the height of the text when wrapped within our max. width.
const QRect R1 = mView->fontMetrics().boundingRect(QRect(0, 0, size.width() - 2 * margin, 0),
Qt::AlignLeft | Qt::TextWordWrap, opt.text);
const QRect R2 = mView->fontMetrics().boundingRect(QRect(0, 0, mView->getWrappedTextWidth(), 0),
Qt::AlignLeft | Qt::TextWordWrap, opt.text);
// compensate for the vertical gap
size.rheight() -= qAbs(R1.height() - R2.height());
//considering the icon size too
size.setWidth(qMax(mView->maxTextWidth(), option.decorationSize.width()));
// adding the margin is good but not necessary because it is already added
size.rwidth() += 2 * margin;
size.rheight() += margin; // only at the bottom
return size;
}
/************************************************
************************************************/
PageSelectWidget::PageSelectWidget(QWidget *parent) :
QListWidget(parent)
, mMaxTextWidth(0)
{
mWrappedTextWidth = fontMetrics().averageCharWidth() * 13; // max. 13 characters
setSelectionRectVisible(false);
setViewMode(IconMode);
setSpacing(2);
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
setWordWrap(true);
setDragEnabled(NoDragDrop);
setEditTriggers(NoEditTriggers);
setTextElideMode(Qt::ElideNone);
setContentsMargins(0, 0, 0, 0);
setItemDelegate(new PageSelectWidgetItemDelegate(this));
connect(model(), &QAbstractItemModel::rowsInserted, this, &PageSelectWidget::updateMaxTextWidth);
connect(model(), &QAbstractItemModel::rowsRemoved, this, &PageSelectWidget::updateMaxTextWidth);
connect(model(), &QAbstractItemModel::dataChanged, this, &PageSelectWidget::updateMaxTextWidth);
}
/************************************************
************************************************/
PageSelectWidget::~PageSelectWidget() = default;
/************************************************
************************************************/
int PageSelectWidget::maxTextWidth() const
{
return mMaxTextWidth;
}
/************************************************
************************************************/
QSize PageSelectWidget::viewportSizeHint() const
{
const int spacing2 = spacing() * 2;
QSize size{sizeHintForColumn(0) + spacing2, (sizeHintForRow(0) + spacing2) * count()};
auto vScrollbar = verticalScrollBar();
if (vScrollbar
&& vScrollbar->isVisible()
&& !vScrollbar->style()->styleHint(QStyle::SH_ScrollBar_Transient, nullptr, vScrollbar))
{
size.rwidth() += verticalScrollBar()->sizeHint().width();
}
return size;
}
/************************************************
************************************************/
QSize PageSelectWidget::sizeHint() const
{
const int f = 2 * frameWidth();
return viewportSizeHint() + QSize(f, f);
}
/************************************************
************************************************/
QSize PageSelectWidget::minimumSizeHint() const
{
return QSize{0, 0};
}
/************************************************
************************************************/
void PageSelectWidget::updateMaxTextWidth()
{
for(int i = count() - 1; 0 <= i; --i)
{
const QRect r = fontMetrics().boundingRect(QRect(0, 0, mWrappedTextWidth, 0),
Qt::AlignLeft | Qt::TextWordWrap, item(i)->text());
mMaxTextWidth = qMax(mMaxTextWidth, r.width());
}
}
/************************************************
************************************************/
bool PageSelectWidget::event(QEvent * event)
{
if (QEvent::StyleChange == event->type())
updateMaxTextWidth();
return QListWidget::event(event);
}
|