File indexing completed on 2024-05-19 12:54:49

0001 /* This file is part of the KDE project
0002    Copyright (C) 2016 Jarosław Staniek <staniek@kde.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "KexiGlobalViewModeSelector.h"
0021 #include "KexiGlobalViewModeSelector_p.h"
0022 #include <KexiStyle.h>
0023 
0024 #include <KLocalizedString>
0025 
0026 #include <QDebug>
0027 #include <QApplication>
0028 #include <QPainter>
0029 #include <QMouseEvent>
0030 
0031 KexiGlobalViewModeSelectorModel::KexiGlobalViewModeSelectorModel(QObject *parent)
0032     : QAbstractListModel(parent)
0033 {
0034 }
0035 
0036 KexiGlobalViewModeSelectorModel::~KexiGlobalViewModeSelectorModel()
0037 {
0038     qDeleteAll(modes);
0039 }
0040 
0041 int KexiGlobalViewModeSelectorModel::rowCount(const QModelIndex &parent) const
0042 {
0043     Q_UNUSED(parent);
0044     return modes.count();
0045 }
0046 
0047 QVariant KexiGlobalViewModeSelectorModel::data(const QModelIndex &index, int role) const
0048 {
0049     if (!index.isValid() || index.row() >= modes.size())
0050         return QVariant();
0051 
0052     KexiGlobalViewModeItem *mode = static_cast<KexiGlobalViewModeItem*>(index.internalPointer());
0053     switch (role) {
0054     case Qt::DisplayRole:
0055         return mode->name;
0056     case Qt::DecorationRole:
0057         return mode->icon;
0058     default:;
0059     }
0060     return QVariant();
0061 }
0062 
0063 Qt::ItemFlags KexiGlobalViewModeSelectorModel::flags(const QModelIndex &index) const
0064 {
0065     Qt::ItemFlags flags = QAbstractListModel::flags(index);
0066     KexiGlobalViewModeItem *mode = static_cast<KexiGlobalViewModeItem*>(index.internalPointer());
0067     if (!mode->enabled) {
0068         flags &= ~(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
0069     }
0070     return flags;
0071 }
0072 
0073 QModelIndex KexiGlobalViewModeSelectorModel::index(int row, int column, const QModelIndex& parent) const
0074 {
0075     Q_UNUSED(parent);
0076     if (row < 0 || row >= modes.count()) {
0077         return QModelIndex();
0078     }
0079     return createIndex(row, column, modes.at(row));
0080 }
0081 
0082 // ----
0083 
0084 KexiGlobalViewModeSelectorDelegate::KexiGlobalViewModeSelectorDelegate(QObject *parent)
0085  : KexiListViewDelegate(parent)
0086 {
0087 }
0088 
0089 void KexiGlobalViewModeSelectorDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
0090                                      const QModelIndex &index) const
0091 {
0092     KexiListViewDelegate::paint(painter, option, index);
0093     KexiStyle::overpaintModeSelectorItem(painter, option, index);
0094 }
0095 
0096 // ----
0097 
0098 class KexiGlobalViewModeSelector::Private
0099 {
0100 public:
0101     Private() : keyboardModifiers(Qt::NoModifier) {}
0102     KexiGlobalViewModeSelectorModel model;
0103     QColor arrowColor;
0104     Qt::KeyboardModifiers keyboardModifiers;
0105 };
0106 
0107 KexiGlobalViewModeSelector::KexiGlobalViewModeSelector(QWidget *parent)
0108  : KexiListView(DontUseDelegate, parent), d(new Private)
0109 {
0110     KexiStyle::setupGlobalViewModeSelector(this);
0111     setSpacing(0);
0112     setContentsMargins(0, 0, 0, 0);
0113     setFocusPolicy(Qt::NoFocus);
0114     setEditTriggers(NoEditTriggers);
0115     setDropIndicatorShown(false);
0116     setSelectionBehavior(SelectRows);
0117     setSelectionMode(SingleSelection);
0118     setSelectionRectVisible(false);
0119     setUniformItemSizes(true);
0120     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
0121     setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
0122 
0123     setItemDelegate(new KexiGlobalViewModeSelectorDelegate(this));
0124 
0125     KexiStyledIconParameters param;
0126     param.color = palette().color(QPalette::Text);
0127     param.selectedColor = palette().color(QPalette::HighlightedText);
0128     param.disabledColor = palette().color(QPalette::Disabled, QPalette::Text);
0129     {
0130         KexiGlobalViewModeItem *welcomeMode = new KexiGlobalViewModeItem;
0131         welcomeMode->name = xi18nc("Welcome global view mode", "Welcome");
0132         param.context = KIconLoader::Action;
0133         param.name = "mode-selector-welcome";
0134         welcomeMode->icon = KexiStyle::icon(param);
0135         d->model.modes << welcomeMode;
0136     }
0137     {
0138         KexiGlobalViewModeItem *projectMode = new KexiGlobalViewModeItem;
0139         projectMode->enabled = false;
0140         projectMode->name = xi18nc("Project global view mode (noun)", "Project");
0141         param.context = KIconLoader::Action;
0142         param.name = "mode-selector-project";
0143         projectMode->icon = KexiStyle::icon(param);
0144         d->model.modes << projectMode;
0145     }
0146     {
0147         KexiGlobalViewModeItem *dataMode = new KexiGlobalViewModeItem;
0148         dataMode->name = xi18nc("Edit global view mode (verb)", "Edit");
0149         param.context = KIconLoader::Action;
0150         param.name = "mode-selector-edit";
0151         dataMode->icon = KexiStyle::icon(param);
0152         d->model.modes << dataMode;
0153     }
0154     {
0155         KexiGlobalViewModeItem *designMode = new KexiGlobalViewModeItem;
0156         designMode->name = xi18nc("Design global view mode (verb)", "Design");
0157         param.context = KIconLoader::Action;
0158         param.name = "mode-selector-design";
0159         designMode->icon = KexiStyle::icon(param);
0160         d->model.modes << designMode;
0161     }
0162     {
0163         KexiGlobalViewModeItem *helpMode = new KexiGlobalViewModeItem;
0164         helpMode->name = xi18nc("Help global view mode (noun)", "Help");
0165         param.context = KIconLoader::Action;
0166         param.name = "mode-selector-help";
0167         helpMode->icon = KexiStyle::icon(param);
0168         d->model.modes << helpMode;
0169     }
0170     setModel(&d->model);
0171     setCurrentMode(Kexi::WelcomeGlobalMode);
0172 }
0173 
0174 KexiGlobalViewModeSelector::~KexiGlobalViewModeSelector()
0175 {
0176 }
0177 
0178 void KexiGlobalViewModeSelector::paintEvent(QPaintEvent *event)
0179 {
0180     KexiListView::paintEvent(event);
0181 
0182     QRect selectedRect;
0183     if (!selectedIndexes().isEmpty()) {
0184         selectedRect = visualRect(selectedIndexes().first());
0185     }
0186     QPainter painter(viewport());
0187     KexiStyle::overpaintGlobalViewModeSelector(this, &painter, selectedRect, d->arrowColor);
0188 }
0189 
0190 Kexi::GlobalViewMode KexiGlobalViewModeSelector::currentMode() const
0191 {
0192     int index = currentIndex().row();
0193     if (index < 0 || index > Kexi::LastGlobalMode) {
0194         index = 0;
0195     }
0196     return static_cast<Kexi::GlobalViewMode>(index);
0197 }
0198 
0199 void KexiGlobalViewModeSelector::setCurrentMode(Kexi::GlobalViewMode mode)
0200 {
0201     if (mode <= Kexi::LastGlobalMode) {
0202         setCurrentIndex(model()->index(int(mode), 0));
0203     }
0204 }
0205 
0206 void KexiGlobalViewModeSelector::currentChanged(const QModelIndex &current, const QModelIndex &previous)
0207 {
0208     const Kexi::GlobalViewMode previousMode = currentMode();
0209     KexiListView::currentChanged(current, previous);
0210     emit currentModeChanged(previousMode);
0211 }
0212 
0213 void KexiGlobalViewModeSelector::mousePressEvent(QMouseEvent *event)
0214 {
0215     d->keyboardModifiers = event->modifiers();
0216     QMouseEvent newEvent = *event;
0217     newEvent.setModifiers(Qt::NoModifier); // otherwise affects selection
0218     KexiListView::mousePressEvent(&newEvent);
0219 }
0220 
0221 void KexiGlobalViewModeSelector::mouseDoubleClickEvent(QMouseEvent *event)
0222 {
0223     Q_UNUSED(event);
0224 }
0225 
0226 void KexiGlobalViewModeSelector::mouseMoveEvent(QMouseEvent *event)
0227 {
0228     Q_UNUSED(event);
0229 }
0230 
0231 Qt::KeyboardModifiers KexiGlobalViewModeSelector::keyboardModifiers() const
0232 {
0233     return d->keyboardModifiers;
0234 }
0235 
0236 void KexiGlobalViewModeSelector::setArrowColor(const QColor &color)
0237 {
0238     d->arrowColor = color;
0239     update(currentIndex());
0240 }