diff --git a/app/layoutsDelegates/activitycmbboxdelegate.cpp b/app/layoutsDelegates/activitycmbboxdelegate.cpp index 0c758e48..042eaae6 100644 --- a/app/layoutsDelegates/activitycmbboxdelegate.cpp +++ b/app/layoutsDelegates/activitycmbboxdelegate.cpp @@ -1,195 +1,207 @@ /* * Copyright 2017-2018 Michail Vourlakos * * This file is part of Latte-Dock * * Latte-Dock is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * Latte-Dock 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "activitycmbboxdelegate.h" #include "../settingsdialog.h" #include #include #include #include #include #include #include #include #include ActivityCmbBoxDelegate::ActivityCmbBoxDelegate(QObject *parent) : QItemDelegate(parent) { auto *settingsDialog = qobject_cast(parent); if (settingsDialog) { m_settingsDialog = settingsDialog; } } QWidget *ActivityCmbBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { - QComboBox *editor = new QComboBox(parent); + QComboBox *editor = new QComboBox(parent); + + //! use focusPolicy as flag in order to update activities only when the user is clicking in the popup + //! it was the only way I found to communicate between the activated (const) signal and the + //! setEditorData (const) function + editor->setFocusPolicy(Qt::StrongFocus); QStringList assignedActivities = index.model()->data(index, Qt::UserRole).toStringList(); QStringList availableActivities = m_settingsDialog->availableActivities(); QStringList activities = m_settingsDialog->activities(); QStringList shownActivities; foreach (auto activity, activities) { if (assignedActivities.contains(activity) || availableActivities.contains(activity)) { shownActivities.append(activity); } } for (unsigned int i = 0; i < shownActivities.count(); ++i) { KActivities::Info info(shownActivities[i]); QString indicator = " "; if (assignedActivities.contains(shownActivities[i])) { indicator = QString::fromUtf8("\u2714") + " "; } if (info.state() != KActivities::Info::Invalid) { editor->addItem(QIcon::fromTheme(info.icon()), QString(indicator + info.name()), QVariant(shownActivities[i])); } } connect(editor, static_cast(&QComboBox::activated), [ = ](int index) { + editor->setFocusPolicy(Qt::ClickFocus); editor->clearFocus(); }); return editor; } void ActivityCmbBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { QComboBox *comboBox = static_cast(editor); QStringList assignedActivities = index.model()->data(index, Qt::UserRole).toStringList(); int pos = -1; if (assignedActivities.count() > 0) { pos = comboBox->findData(QVariant(assignedActivities[0])); } comboBox->setCurrentIndex(pos); } void ActivityCmbBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { QComboBox *comboBox = static_cast(editor); + if (editor->focusPolicy() != Qt::ClickFocus) { + return; + } + + editor->setFocusPolicy(Qt::StrongFocus); + QStringList assignedActivities = index.model()->data(index, Qt::UserRole).toStringList(); QString selectedActivity = comboBox->currentData().toString(); if (assignedActivities.contains(selectedActivity)) { assignedActivities.removeAll(selectedActivity); } else { assignedActivities.append(selectedActivity); } model->setData(index, assignedActivities, Qt::UserRole); } void ActivityCmbBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const { editor->setGeometry(option.rect); } void ActivityCmbBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QStyleOptionViewItem myOptions = option; painter->save(); QStringList assignedActivities = index.model()->data(index, Qt::UserRole).toStringList(); if (assignedActivities.count() > 0) { myOptions.text = assignedActivitiesText(index); QTextDocument doc; QString css; QString activitiesText = myOptions.text; QBrush nBrush; if ((option.state & QStyle::State_Active) && (option.state & QStyle::State_Selected)) { nBrush = option.palette.brush(QPalette::Active, QPalette::HighlightedText); } else { nBrush = option.palette.brush(QPalette::Inactive, QPalette::Text); } css = QString("body { color : %1; }").arg(nBrush.color().name()); doc.setDefaultStyleSheet(css); doc.setHtml("" + myOptions.text + ""); myOptions.text = ""; myOptions.widget->style()->drawControl(QStyle::CE_ItemViewItem, &myOptions, painter); //we need an offset to be in the same vertical center of TextEdit int offsetY = ((myOptions.rect.height() - doc.size().height()) / 2); if ((qApp->layoutDirection() == Qt::RightToLeft) && !activitiesText.isEmpty()) { int textWidth = doc.size().width(); painter->translate(qMax(myOptions.rect.left(), myOptions.rect.right() - textWidth), myOptions.rect.top() + offsetY + 1); } else { painter->translate(myOptions.rect.left(), myOptions.rect.top() + offsetY + 1); } QRect clip(0, 0, myOptions.rect.width(), myOptions.rect.height()); doc.drawContents(painter, clip); } else { QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &myOptions, painter); } painter->restore(); } QString ActivityCmbBoxDelegate::assignedActivitiesText(const QModelIndex &index) const { QStringList assignedActivities = index.model()->data(index, Qt::UserRole).toStringList(); QString finalText; if (assignedActivities.count() > 0) { for (int i = 0; i < assignedActivities.count(); ++i) { KActivities::Info info(assignedActivities[i]); if (info.state() != KActivities::Info::Invalid) { if (i > 0) { finalText += ", "; } bool isActive{false}; if ((info.state() == KActivities::Info::Running) || (info.state() == KActivities::Info::Starting)) { isActive = true; } finalText += isActive ? "" + info.name() + "" : info.name(); } } } return finalText; } diff --git a/app/layoutsDelegates/checkboxdelegate.cpp b/app/layoutsDelegates/checkboxdelegate.cpp index 76876af1..76b89848 100644 --- a/app/layoutsDelegates/checkboxdelegate.cpp +++ b/app/layoutsDelegates/checkboxdelegate.cpp @@ -1,76 +1,76 @@ /* * Copyright 2017-2018 Michail Vourlakos * * This file is part of Latte-Dock * * Latte-Dock is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * Latte-Dock 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "checkboxdelegate.h" #include #include #include #include #include #include #include const int HIDDENTEXTCOLUMN = 1; CheckBoxDelegate::CheckBoxDelegate(QObject *parent) : QStyledItemDelegate(parent) { } void CheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QStandardItemModel *model = (QStandardItemModel *) index.model(); QStyledItemDelegate::paint(painter, option, model->index(index.row(), HIDDENTEXTCOLUMN)); QStyledItemDelegate::paint(painter, option, index); } bool CheckBoxDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) { Q_ASSERT(event); Q_ASSERT(model); // // make sure that the item is checkable // Qt::ItemFlags flags = model->flags(index); // // if (!(flags & Qt::ItemIsUserCheckable) || !(flags & Qt::ItemIsEnabled)) // return false; // // // make sure that we have a check state QString value{index.data().toString()}; // // if (!value.isValid()) // return false; // make sure that we have the right event type - if (event->type() == QEvent::MouseButtonRelease) { + if (event->type() == QEvent::MouseButtonDblClick) { if (!option.rect.contains(static_cast(event)->pos())) return false; } else if (event->type() == QEvent::KeyPress) { if (static_cast(event)->key() != Qt::Key_Space && static_cast(event)->key() != Qt::Key_Select) return false; } else { return false; } const QChar CheckMark{0x2714}; return model->setData(index, value == CheckMark ? QString("") : CheckMark, Qt::DisplayRole); } diff --git a/app/layoutsDelegates/colorcmbboxdelegate.cpp b/app/layoutsDelegates/colorcmbboxdelegate.cpp index 12556c47..f974e7c8 100644 --- a/app/layoutsDelegates/colorcmbboxdelegate.cpp +++ b/app/layoutsDelegates/colorcmbboxdelegate.cpp @@ -1,156 +1,156 @@ /* * Copyright 2017-2018 Michail Vourlakos * * This file is part of Latte-Dock * * Latte-Dock is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * Latte-Dock 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "colorcmbboxdelegate.h" #include "colorcmbboxitemdelegate.h" #include "../settingsdialog.h" #include #include #include #include #include #include #include #include #include #include ColorCmbBoxDelegate::ColorCmbBoxDelegate(QObject *parent, QString iconsPath, QStringList colors) : QItemDelegate(parent), m_parent(parent), m_iconsPath(iconsPath), Colors(colors) { } QWidget *ColorCmbBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { QComboBox *editor = new QComboBox(parent); editor->setItemDelegate(new ColorCmbBoxItemDelegate(editor, m_iconsPath)); for (unsigned int i = 0; i < Colors.count(); ++i) { if (Colors[i] != "sepia") { QPixmap pixmap(50, 50); pixmap.fill(QColor(Colors[i])); QIcon icon(pixmap); editor->addItem(icon, Colors[i]); } } QString value = index.model()->data(index, Qt::BackgroundRole).toString(); const QModelIndex &indexOriginal = index; bool showTextColor{false}; //! add the background if exists if (value.startsWith("/")) { QIcon icon(value); editor->addItem(icon, value); showTextColor = true; } editor->addItem(" " + i18n("Select image..."), "select_image"); if (showTextColor) { editor->addItem(" " + i18n("Text color..."), "text_color"); } connect(editor, static_cast(&QComboBox::activated), [ = ](int index) { editor->clearFocus(); if ((showTextColor && index == editor->count() - 2) || (!showTextColor && index == editor->count() - 1)) { Latte::SettingsDialog *settings = qobject_cast(m_parent); if (settings) { settings->requestImagesDialog(indexOriginal.row()); } } else if (showTextColor && index == editor->count() - 1) { Latte::SettingsDialog *settings = qobject_cast(m_parent); if (settings) { settings->requestColorsDialog(indexOriginal.row()); } } }); return editor; } void ColorCmbBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { QComboBox *comboBox = static_cast(editor); QString value = index.model()->data(index, Qt::BackgroundRole).toString(); QString userData = index.model()->data(index, Qt::UserRole).toString(); int pos = Colors.indexOf(value); if (pos == -1 && value.startsWith("/")) { - comboBox->setCurrentIndex(Colors.count() - 1); + comboBox->setCurrentIndex(Colors.count()); } else { comboBox->setCurrentIndex(Colors.indexOf(value)); } } void ColorCmbBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { QComboBox *comboBox = static_cast(editor); QString itemData = comboBox->currentData().toString(); if (itemData != "select_image" && itemData != "text_color") { model->setData(index, comboBox->currentText(), Qt::BackgroundRole); } } void ColorCmbBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const { editor->setGeometry(option.rect); } void ColorCmbBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QStyleOptionViewItem myOption = option; QVariant value = index.data(Qt::BackgroundRole); QVariant data = index.data(Qt::UserRole); QString dataStr = data.toString(); if (value.isValid() && (dataStr != "select_image") && (dataStr != "text_color")) { QString valueStr = value.toString(); QString colorPath = valueStr.startsWith("/") ? valueStr : m_iconsPath + value.toString() + "print.jpg"; if (QFileInfo(colorPath).exists()) { QBrush colorBrush; colorBrush.setTextureImage(QImage(colorPath).scaled(QSize(50, 50))); painter->setBrush(colorBrush); painter->drawRect(QRect(option.rect.x(), option.rect.y(), option.rect.width(), option.rect.height())); } } //QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &myOption, painter); }