diff --git a/src/kexiutils/KexiStyle.cpp b/src/kexiutils/KexiStyle.cpp index 5e4c2aff8..41058fdc0 100644 --- a/src/kexiutils/KexiStyle.cpp +++ b/src/kexiutils/KexiStyle.cpp @@ -1,239 +1,241 @@ /* This file is part of the KDE project Copyright (C) 2016 Jarosław Staniek This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "KexiStyle.h" #include "utils.h" #include #include #include #include #include #include #include namespace KexiStyle { KEXIUTILS_EXPORT void setupFrame(QFrame *frame) { if (frame) { frame->setFrameStyle(QFrame::NoFrame); } } KEXIUTILS_EXPORT void setupModeSelector(QFrame *selector) { KexiStyle::setupFrame(selector); selector->setFont(KexiUtils::smallestReadableFont()); QPalette p(selector->palette()); p.setColor(QPalette::Window, KexiUtils::shadeBlack()); p.setColor(QPalette::Base, KexiUtils::shadeBlack()); p.setColor(QPalette::Button, KexiUtils::shadeBlack()); p.setColor(QPalette::AlternateBase, KexiUtils::shadeBlackLighter()); p.setColor(QPalette::WindowText, KexiUtils::cardboardGrey()); p.setColor(QPalette::ButtonText, KexiUtils::cardboardGrey()); p.setColor(QPalette::Text, KexiUtils::cardboardGrey()); + p.setBrush(QPalette::Disabled, QPalette::Text, KexiUtils::iconGrey()); // yes, modes can be disabled + //generic?? KColorScheme(QPalette::Disabled, KColorScheme::View).foreground()); p.setColor(QPalette::Highlight, KexiUtils::cardboardGrey()); p.setColor(QPalette::Active, QPalette::Highlight, KexiUtils::plasmaBlue()); // unused anyway because mode selector has no focus p.setColor(QPalette::HighlightedText, KexiUtils::charcoalGrey()); selector->setPalette(p); } KEXIUTILS_EXPORT void overpaintModeSelector(QWidget *widget, QPainter *painter, const QRect &selectedRect) { // draw gradient painter->save(); int w = widget->fontMetrics().height() * 3 / 2; painter->translate(widget->width() - w, 0); QLinearGradient grad(0, 0, w, 0); QColor c(widget->palette().base().color()); c.setAlpha(0); grad.setColorAt(0, c); c.setAlpha(15); grad.setColorAt(0.5, c); grad.setColorAt(1.0, QColor(0, 0, 0, 50)); painter->fillRect(0, 0, w, widget->height(), QBrush(grad)); painter->restore(); // draw: /| // \| if (!selectedRect.isNull()) { painter->save(); w = selectedRect.height() / 10; if (w % 2 == 0) { ++w; } painter->translate(selectedRect.x() + selectedRect.width() - w, selectedRect.y() + (selectedRect.height() - w * 2) / 2 - 0.5); QPolygon polygon; polygon << QPoint(w, 0) << QPoint(w, w * 2) << QPoint(0, w); painter->setPen(QPen(Qt::NoPen)); painter->setBrush(KexiUtils::charcoalGrey()); painter->drawPolygon(polygon); painter->restore(); } } KEXIUTILS_EXPORT void overpaintModeSelectorItem(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) { Q_UNUSED(painter) Q_UNUSED(option) Q_UNUSED(index) } KEXIUTILS_EXPORT QPalette alternativePalette(const QPalette &palette) { QPalette p(palette); p.setColor(QPalette::Window, KexiUtils::charcoalGrey()); p.setColor(QPalette::Base, KexiUtils::shadeBlack()); p.setColor(QPalette::Button, KexiUtils::shadeBlack()); p.setColor(QPalette::AlternateBase, KexiUtils::shadeBlackLighter()); p.setColor(QPalette::WindowText, KexiUtils::paperWhite()); p.setColor(QPalette::ButtonText, KexiUtils::paperWhite()); p.setColor(QPalette::Text, KexiUtils::paperWhite()); p.setColor(QPalette::Highlight, KexiUtils::cardboardGrey()); p.setColor(QPalette::Active, QPalette::Highlight, KexiUtils::plasmaBlue()); p.setColor(QPalette::HighlightedText, KexiUtils::charcoalGrey()); p.setColor(QPalette::Active, QPalette::HighlightedText, KexiUtils::cardboardGrey()); return p; } KEXIUTILS_EXPORT QPalette sidebarsPalette(const QPalette &palette) { return alternativePalette(palette); } KEXIUTILS_EXPORT void setSidebarsPalette(QWidget *widget) { widget->setPalette(sidebarsPalette(widget->palette())); widget->setAutoFillBackground(true); } KEXIUTILS_EXPORT QFont titleFont(const QFont &font) { QFont newFont(font); newFont.setCapitalization(QFont::AllUppercase); return newFont; } static const QString g_contexts[] = { QLatin1String("actions"), // Any QLatin1String("actions"), QLatin1String("apps"), QLatin1String("devices"), QLatin1String("filesystems"), QLatin1String("mimetypes"), QLatin1String("animations"), QLatin1String("categories"), QLatin1String("emblems"), QLatin1String("emotes"), QLatin1String("intl"), QLatin1String("places"), QLatin1String("status") }; KEXIUTILS_EXPORT QIcon darkIcon(const QString &iconName, KIconLoader::Context iconContext) { Q_ASSERT(iconContext < (sizeof(g_contexts) / sizeof(g_contexts[0]))); static const QIcon::Mode modes[] = { QIcon::Normal }; //can be supported too: , QIcon::Selected }; const QString prefix(QLatin1String(":/icons/breeze/") + g_contexts[iconContext] + QLatin1Char('/')); const QString suffixes[] = { iconName + QLatin1String("@dark.svg"), iconName + QLatin1String(".svg") }; static const QString sizesStr[] = { QString::fromLatin1("32/"), // important: opposite direction QString::fromLatin1("22/"), QString::fromLatin1("16/") }; static const QSize sizes[] = { QSize(32, 32), QSize(22, 22), QSize(16, 16) }; // important: opposite direction QIcon icon; for (int mode = 0; mode < int(sizeof(modes) / sizeof(modes[0])); ++mode) { for (int size = 0; size < int(sizeof(sizes) / sizeof(sizes[0])); ++size) { //qDebug() << prefix + sizesStr[size] + suffixes[mode] << sizes[size] << modes[mode]; icon.addFile(prefix + sizesStr[size] + suffixes[mode], sizes[size], modes[mode], QIcon::Off); icon.addFile(prefix + sizesStr[size] + suffixes[mode], sizes[size], modes[mode], QIcon::On); } } return icon; } class IconEngine : public QIconEngine { public: IconEngine(const KexiStyledIconParameters ¶meters) : m_parameters(parameters) { } inline QIconEngine *clone() const Q_DECL_OVERRIDE { return new IconEngine(*this); } //! @todo add caching? QPixmap pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) Q_DECL_OVERRIDE { Q_UNUSED(state) QFile f(QLatin1String(":/icons/breeze/") + g_contexts[m_parameters.context] + QChar('/') + QString::number(size.width()) + QChar('/') + m_parameters.name + ".svg"); if (!f.open(QIODevice::ReadOnly)) { return QPixmap(); } QByteArray svg(f.readAll()); QColor color; if (mode == QIcon::Selected && m_parameters.selectedColor.isValid()) { color = m_parameters.selectedColor; } else if (mode == QIcon::Disabled && m_parameters.disabledColor.isValid()) { color = m_parameters.disabledColor; qDebug() << m_parameters.disabledColor; } else if (m_parameters.color.isValid()) { color = m_parameters.color; } if (color.isValid()) { svg.replace(KexiUtils::iconGrey().name().toLatin1(), color.name().toLatin1()); } QSvgRenderer renderer(svg); QPixmap pm(size); pm.fill(Qt::transparent); QPainter p(&pm); renderer.render(&p, pm.rect()); return pm; } //! Nothing to paint extra here void paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state) Q_DECL_OVERRIDE { Q_UNUSED(painter) Q_UNUSED(rect) Q_UNUSED(mode) Q_UNUSED(state) } private: //! Needed for clone() IconEngine(const IconEngine &other) : m_parameters(other.m_parameters) {} const KexiStyledIconParameters m_parameters; }; KEXIUTILS_EXPORT QIcon icon(const KexiStyledIconParameters ¶meters) { return QIcon(new IconEngine(parameters)); } } diff --git a/src/main/KexiModeSelector.cpp b/src/main/KexiModeSelector.cpp index 7352caca1..a7a437deb 100644 --- a/src/main/KexiModeSelector.cpp +++ b/src/main/KexiModeSelector.cpp @@ -1,146 +1,176 @@ /* This file is part of the KDE project Copyright (C) 2016 Jarosław Staniek This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "KexiModeSelector.h" #include #include +#include #include #include KexiModeSelectorModel::KexiModeSelectorModel(QObject *parent) : QAbstractListModel(parent) { } +KexiModeSelectorModel::~KexiModeSelectorModel() +{ + qDeleteAll(modes); +} + int KexiModeSelectorModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent); return modes.count(); } QVariant KexiModeSelectorModel::data(const QModelIndex &index, int role) const { if (!index.isValid() || index.row() >= modes.size()) return QVariant(); - if (role == Qt::DisplayRole) { - return modes.at(index.row()).name; - } else if (role == Qt::DecorationRole) { - return modes.at(index.row()).icon; + KexiMode *mode = static_cast(index.internalPointer()); + switch (role) { + case Qt::DisplayRole: + return mode->name; + case Qt::DecorationRole: + return mode->icon; + default:; } return QVariant(); } +Qt::ItemFlags KexiModeSelectorModel::flags(const QModelIndex &index) const +{ + Qt::ItemFlags flags = QAbstractListModel::flags(index); + KexiMode *mode = static_cast(index.internalPointer()); + if (!mode->enabled) { + flags &= ~(Qt::ItemIsEnabled | Qt::ItemIsSelectable); + } + return flags; +} + +QModelIndex KexiModeSelectorModel::index(int row, int column, const QModelIndex& parent) const +{ + Q_UNUSED(parent); + if (row < 0 || row >= modes.count()) { + return QModelIndex(); + } + return createIndex(row, column, modes.at(row)); +} + // ---- KexiModeSelectorDelegate::KexiModeSelectorDelegate(QObject *parent) : KexiListViewDelegate(parent) { } void KexiModeSelectorDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { KexiListViewDelegate::paint(painter, option, index); KexiStyle::overpaintModeSelectorItem(painter, option, index); } // ---- KexiModeSelector::KexiModeSelector(QWidget *parent) : KexiListView(DontUseDelegate, parent) { KexiStyle::setupModeSelector(this); setSpacing(0); setContentsMargins(0, 0, 0, 0); setFocusPolicy(Qt::NoFocus); setEditTriggers(NoEditTriggers); setDropIndicatorShown(false); setSelectionBehavior(SelectRows); setSelectionRectVisible(false); setUniformItemSizes(true); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); setItemDelegate(new KexiModeSelectorDelegate(this)); KexiStyledIconParameters param; param.color = palette().color(QPalette::Text); param.selectedColor = palette().color(QPalette::HighlightedText); + param.disabledColor = palette().color(QPalette::Disabled, QPalette::Text); { - KexiMode welcomeMode; - welcomeMode.name = xi18nc("Welcome mode", "Welcome"); + KexiMode *welcomeMode = new KexiMode; + welcomeMode->name = xi18nc("Welcome mode", "Welcome"); param.context = KIconLoader::Action; param.name = "mode-selector-welcome"; - welcomeMode.icon = KexiStyle::icon(param); + welcomeMode->icon = KexiStyle::icon(param); m_model.modes << welcomeMode; } { - KexiMode projectMode; - projectMode.name = xi18nc("Project mode", "Project"); + KexiMode *projectMode = new KexiMode; + projectMode->enabled = false; + projectMode->name = xi18nc("Project mode", "Project"); param.context = KIconLoader::Action; param.name = "mode-selector-project"; - projectMode.icon = KexiStyle::icon(param); + projectMode->icon = KexiStyle::icon(param); m_model.modes << projectMode; } { - KexiMode dataMode; - dataMode.name = xi18nc("Data mode", "Data"); + KexiMode *dataMode = new KexiMode; + dataMode->name = xi18nc("Data mode", "Data"); param.context = KIconLoader::Action; param.name = "mode-selector-data"; - dataMode.icon = KexiStyle::icon(param); + dataMode->icon = KexiStyle::icon(param); m_model.modes << dataMode; } { - KexiMode designMode; - designMode.name = xi18nc("Design mode", "Design"); + KexiMode *designMode = new KexiMode; + designMode->name = xi18nc("Design mode", "Design"); param.context = KIconLoader::Action; param.name = "mode-selector-design"; - designMode.icon = KexiStyle::icon(param); + designMode->icon = KexiStyle::icon(param); m_model.modes << designMode; } { - KexiMode helpMode; - helpMode.name = xi18nc("Help mode", "Help"); + KexiMode *helpMode = new KexiMode; + helpMode->name = xi18nc("Help mode", "Help"); param.context = KIconLoader::Action; param.name = "mode-selector-help"; - helpMode.icon = KexiStyle::icon(param); + helpMode->icon = KexiStyle::icon(param); m_model.modes << helpMode; } setModel(&m_model); selectionModel()->select(model()->index(0, 0), QItemSelectionModel::Select); } KexiModeSelector::~KexiModeSelector() { } void KexiModeSelector::paintEvent(QPaintEvent *event) { KexiListView::paintEvent(event); QRect selectedRect; if (!selectedIndexes().isEmpty()) { selectedRect = visualRect(selectedIndexes().first()); } QPainter painter(viewport()); KexiStyle::overpaintModeSelector(this, &painter, selectedRect); } diff --git a/src/main/KexiModeSelector.h b/src/main/KexiModeSelector.h index 3f2b5de09..da652f66b 100644 --- a/src/main/KexiModeSelector.h +++ b/src/main/KexiModeSelector.h @@ -1,75 +1,83 @@ /* This file is part of the KDE project Copyright (C) 2016 Jarosław Staniek This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #ifndef KEXIMODESELECTOR_H #define KEXIMODESELECTOR_H #include class QPainter; //! @internal A single global mode class KexiMode { public: + KexiMode() : enabled(true) {} QString name; QIcon icon; + bool enabled; }; //! @internal A model for KexiModeSelector, each item has name and icon class KexiModeSelectorModel : public QAbstractListModel { Q_OBJECT public: explicit KexiModeSelectorModel(QObject *parent = 0); - int rowCount(const QModelIndex &parent = QModelIndex()) const; + ~KexiModeSelectorModel(); - QVariant data(const QModelIndex &index, int role) const; + int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; - QList modes; + QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE; + + Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE; + + QModelIndex index(int row, int column, const QModelIndex& parent) const Q_DECL_OVERRIDE; + + QList modes; }; //! @internal A delegate for items of KexiModeSelector class KexiModeSelectorDelegate : public KexiListViewDelegate { Q_OBJECT public: explicit KexiModeSelectorDelegate(QObject *parent = 0); void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE; }; //! @internal Global mode selector class KexiModeSelector : public KexiListView { public: explicit KexiModeSelector(QWidget *parent = 0); virtual ~KexiModeSelector(); protected: void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; KexiModeSelectorModel m_model; }; #endif // KEXIMODESELECTOR_H diff --git a/src/widget/KexiListView.cpp b/src/widget/KexiListView.cpp index fbbe7240d..bc6d4b3ca 100644 --- a/src/widget/KexiListView.cpp +++ b/src/widget/KexiListView.cpp @@ -1,262 +1,275 @@ /* This file is part of the KDE project Copyright (C) 2016 Jarosław Staniek Forked from kwidgetsaddons/src/kpageview_p.cpp: Copyright (C) 2006 Tobias Koenig (tokoe@kde.org) Copyright (C) 2007 Rafael Fernández López (ereslibre@kde.org) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "KexiListView.h" #include "KexiListView_p.h" #include +#include #include #include const int KEXILISTVIEW_VERTICAL_MARGIN = 10; const int KEXILISTVIEW_HORIZONTAL_MARGIN = 12; KexiListView::KexiListView(QWidget *parent) : KexiListView(UseDefaultDelegate, parent) { } KexiListView::KexiListView(UseDelegate useDelegate, QWidget *parent) : QListView(parent) { setViewMode(QListView::ListMode); setMovement(QListView::Static); setVerticalScrollMode(QListView::ScrollPerPixel); if (useDelegate == UseDefaultDelegate) { setItemDelegate(new KexiListViewDelegate(this)); } } KexiListView::~KexiListView() { } void KexiListView::setModel(QAbstractItemModel *model) { /* KPageListViewProxy *proxy = new KPageListViewProxy( this ); proxy->setSourceModel( model ); proxy->rebuildMap(); connect( model, SIGNAL(layoutChanged()), proxy, SLOT(rebuildMap()) ); */ connect(model, SIGNAL(layoutChanged()), this, SLOT(updateWidth())); // QListView::setModel( proxy ); QListView::setModel(model); // Set our own selection model, which won't allow our current selection to be cleared setSelectionModel(new KexiListViewSelectionModel(model, this)); updateWidth(); } void KexiListView::updateWidth() { if (!model()) { return; } int rows = model()->rowCount(); int width = 0; for (int i = 0; i < rows; ++i) { width = qMax(width, sizeHintForIndex(model()->index(i, 0)).width()); } setFixedWidth(width + KEXILISTVIEW_HORIZONTAL_MARGIN * 2); } // ---- KexiListViewDelegate::KexiListViewDelegate(QObject *parent) : QAbstractItemDelegate(parent) { } static int layoutText(QTextLayout *layout, int maxWidth) { qreal height = 0; int textWidth = 0; layout->beginLayout(); while (true) { QTextLine line = layout->createLine(); if (!line.isValid()) { break; } line.setLineWidth(maxWidth); line.setPosition(QPointF(0, height)); height += line.height(); textWidth = qMax(textWidth, qRound(line.naturalTextWidth() + 0.5)); } layout->endLayout(); return textWidth; } void KexiListViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { if (!index.isValid()) { return; } QStyleOptionViewItem opt(option); opt.showDecorationSelected = true; - QStyle *style = opt.widget ? opt.widget->style() : QApplication::style(); + const QStyle *style = opt.widget ? opt.widget->style() : QApplication::style(); + paint(painter, *style, &opt, index); +} - int iconSize = style->pixelMetric(QStyle::PM_IconViewIconSize); +void KexiListViewDelegate::paint(QPainter *painter, const QStyle &style, + QStyleOptionViewItem *option, + const QModelIndex &index) const +{ + int iconSize = style.pixelMetric(QStyle::PM_IconViewIconSize); const QString text = index.model()->data(index, Qt::DisplayRole).toString(); const QIcon icon = index.model()->data(index, Qt::DecorationRole).value(); - const QPixmap pixmap = icon.pixmap(iconSize, iconSize, - (option.state & QStyle::State_Selected) ? QIcon::Selected : QIcon::Normal); - + QIcon::Mode iconMode; + if (option->state & QStyle::State_Enabled && option->state & QStyle::State_Selected) { + iconMode = QIcon::Selected; + } else { + iconMode = (option->state & QStyle::State_Enabled) ? QIcon::Normal : QIcon::Disabled; + } + const QPixmap pixmap = icon.pixmap(iconSize, iconSize, iconMode); QFontMetrics fm = painter->fontMetrics(); int wp = pixmap.width() / pixmap.devicePixelRatio(); int hp = pixmap.height() / pixmap.devicePixelRatio(); - QTextLayout iconTextLayout(text, option.font); + QTextLayout iconTextLayout(text, option->font); QTextOption textOption(Qt::AlignHCenter); iconTextLayout.setTextOption(textOption); int maxWidth = qMax(3 * wp, 8 * fm.height()); layoutText(&iconTextLayout, maxWidth); QPen pen = painter->pen(); - QPalette::ColorGroup cg = option.state & QStyle::State_Enabled - ? QPalette::Normal : QPalette::Disabled; - if (cg == QPalette::Normal && !(option.state & QStyle::State_Active)) { - cg = QPalette::Inactive; + QPalette::ColorGroup cg; + if (option->state & QStyle::State_Enabled) { + cg = (option->state & QStyle::State_Active) ? QPalette::Normal : QPalette::Inactive; + } else { + cg = QPalette::Disabled; + option->state &= ~QStyle::State_MouseOver; } + //qDebug() << hex << int(option->state) << text << int(iconMode) << cg; - style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget); - if (option.state & QStyle::State_Selected) { - painter->setPen(option.palette.color(cg, QPalette::HighlightedText)); + style.drawPrimitive(QStyle::PE_PanelItemViewItem, option, painter, option->widget); + if (option->state & QStyle::State_Selected) { + painter->setPen(option->palette.color(cg, QPalette::HighlightedText)); } else { - painter->setPen(option.palette.color(cg, QPalette::Text)); + painter->setPen(option->palette.color(cg, QPalette::Text)); } - painter->drawPixmap(option.rect.x() + (option.rect.width() / 2) - - (wp / 2), option.rect.y() + KEXILISTVIEW_VERTICAL_MARGIN, + painter->drawPixmap(option->rect.x() + (option->rect.width() / 2) + - (wp / 2), option->rect.y() + KEXILISTVIEW_VERTICAL_MARGIN, pixmap); if (!text.isEmpty()) { iconTextLayout.draw(painter, - QPoint(option.rect.x() + (option.rect.width() / 2) - - (maxWidth / 2), option.rect.y() + hp + KEXILISTVIEW_VERTICAL_MARGIN + 2)); + QPoint(option->rect.x() + (option->rect.width() / 2) + - (maxWidth / 2), option->rect.y() + hp + KEXILISTVIEW_VERTICAL_MARGIN + 2)); } painter->setPen(pen); - - drawFocus(painter, option, option.rect); + drawFocus(painter, *option, option->rect); } QSize KexiListViewDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { if (!index.isValid()) { return QSize(0, 0); } QStyleOptionViewItem opt(option); opt.showDecorationSelected = true; QStyle *style = opt.widget ? opt.widget->style() : QApplication::style(); int iconSize = style->pixelMetric(QStyle::PM_IconViewIconSize); const QString text = index.model()->data(index, Qt::DisplayRole).toString(); const QIcon icon = index.model()->data(index, Qt::DecorationRole).value(); const QPixmap pixmap = icon.pixmap(iconSize, iconSize); QFontMetrics fm = option.fontMetrics; int gap = 0; //fm.height(); int wp = pixmap.width() / pixmap.devicePixelRatio(); int hp = pixmap.height() / pixmap.devicePixelRatio(); if (hp == 0) { /** * No pixmap loaded yet, we'll use the default icon size in this case. */ hp = iconSize; wp = iconSize; } QTextLayout iconTextLayout(text, option.font); int wt = layoutText(&iconTextLayout, qMax(3 * wp, 8 * fm.height())); int ht = iconTextLayout.boundingRect().height(); int width, height; if (text.isEmpty()) { height = hp; } else { height = hp + ht + 2 * KEXILISTVIEW_VERTICAL_MARGIN; } width = qMax(wt, wp) + gap; return QSize(width, height); } void KexiListViewDelegate::drawFocus(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect) const { if (option.state & QStyle::State_HasFocus) { QStyleOptionFocusRect o; o.QStyleOption::operator=(option); o.rect = rect; o.state |= QStyle::State_KeyboardFocusChange; QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ? QPalette::Normal : QPalette::Disabled; o.backgroundColor = option.palette.color(cg, (option.state & QStyle::State_Selected) ? QPalette::Highlight : QPalette::Background); QApplication::style()->drawPrimitive(QStyle::PE_FrameFocusRect, &o, painter); } } // ---- KexiListViewSelectionModel::KexiListViewSelectionModel(QAbstractItemModel *model, QObject *parent) : QItemSelectionModel(model, parent) { } void KexiListViewSelectionModel::clear() { // Don't allow the current selection to be cleared } void KexiListViewSelectionModel::select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) { // Don't allow the current selection to be cleared if (!index.isValid() && (command & QItemSelectionModel::Clear)) { return; } QItemSelectionModel::select(index, command); } void KexiListViewSelectionModel::select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command) { // Don't allow the current selection to be cleared if (!selection.count() && (command & QItemSelectionModel::Clear)) { return; } QItemSelectionModel::select(selection, command); } diff --git a/src/widget/KexiListView.h b/src/widget/KexiListView.h index 6b1c4e57a..1c016278f 100644 --- a/src/widget/KexiListView.h +++ b/src/widget/KexiListView.h @@ -1,75 +1,77 @@ /* This file is part of the KDE project Copyright (C) 2016 Jarosław Staniek Forked from kwidgetsaddons/src/kpageview_p.h: Copyright (C) 2006 Tobias Koenig (tokoe@kde.org) Copyright (C) 2007 Rafael Fernández López (ereslibre@kde.org) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #ifndef KEXILISTVIEW_H #define KEXILISTVIEW_H #include #include "kexiextwidgets_export.h" //! Default delegate for KexiListView class KEXIEXTWIDGETS_EXPORT KexiListViewDelegate : public QAbstractItemDelegate { Q_OBJECT public: explicit KexiListViewDelegate(QObject *parent = 0); void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE; QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE; private: void drawFocus(QPainter *, const QStyleOptionViewItem &, const QRect &) const; + void paint(QPainter *painter, const QStyle &style, QStyleOptionViewItem *option, + const QModelIndex &index) const; }; class KEXIEXTWIDGETS_EXPORT KexiListView : public QListView { Q_OBJECT public: explicit KexiListView(QWidget *parent = 0); virtual ~KexiListView(); void setModel(QAbstractItemModel *model) Q_DECL_OVERRIDE; protected: //! Used in KexiListView(UseDelegate, QWidget*) enum UseDelegate { UseDefaultDelegate, DontUseDelegate }; //! Alternative constructor, the same as the default but if @a useDelegate is DontUseDelegate, //! delegate is not set. This allows to replace delegate. KexiListView(UseDelegate useDelegate = UseDefaultDelegate, QWidget *parent = 0); private Q_SLOTS: void updateWidth(); }; #endif