mirror of
https://github.com/KDE/latte-dock.git
synced 2025-03-30 14:50:12 +03:00
introduce sharedcmbboxdelegate at settings ui
This commit is contained in:
parent
a52144cc3a
commit
1b21e26dfa
@ -5,5 +5,6 @@ set(lattedock-app_SRCS
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/colorcmbboxdelegate.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/colorcmbboxitemdelegate.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/layoutnamedelegate.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/sharedcmbboxdelegate.cpp
|
||||
PARENT_SCOPE
|
||||
)
|
||||
|
203
app/settings/delegates/sharedcmbboxdelegate.cpp
Normal file
203
app/settings/delegates/sharedcmbboxdelegate.cpp
Normal file
@ -0,0 +1,203 @@
|
||||
/*
|
||||
* Copyright 2019 Michail Vourlakos <mvourlakos@gmail.com>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "sharedcmbboxdelegate.h"
|
||||
|
||||
// local
|
||||
#include "../settingsdialog.h"
|
||||
|
||||
// Qt
|
||||
#include <QApplication>
|
||||
#include <QComboBox>
|
||||
#include <QDebug>
|
||||
#include <QWidget>
|
||||
#include <QModelIndex>
|
||||
#include <QPainter>
|
||||
#include <QString>
|
||||
#include <QTextDocument>
|
||||
|
||||
// KDE
|
||||
#include <KActivities/Info>
|
||||
|
||||
SharedCmbBoxDelegate::SharedCmbBoxDelegate(QObject *parent)
|
||||
: QItemDelegate(parent)
|
||||
{
|
||||
auto *settingsDialog = qobject_cast<Latte::SettingsDialog *>(parent);
|
||||
|
||||
if (settingsDialog) {
|
||||
m_settingsDialog = settingsDialog;
|
||||
}
|
||||
}
|
||||
|
||||
QWidget *SharedCmbBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||
{
|
||||
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;
|
||||
|
||||
for (const 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<void(QComboBox::*)(int)>(&QComboBox::activated), [ = ](int index) {
|
||||
editor->setFocusPolicy(Qt::ClickFocus);
|
||||
editor->clearFocus();
|
||||
});
|
||||
*/
|
||||
return editor;
|
||||
}
|
||||
|
||||
void SharedCmbBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
|
||||
{
|
||||
QComboBox *comboBox = static_cast<QComboBox *>(editor);
|
||||
QStringList assignedLayouts = index.model()->data(index, Qt::UserRole).toStringList();
|
||||
|
||||
int pos = -1;
|
||||
|
||||
if (assignedLayouts.count() > 0) {
|
||||
pos = comboBox->findData(QVariant(assignedLayouts[0]));
|
||||
}
|
||||
|
||||
comboBox->setCurrentIndex(pos);
|
||||
}
|
||||
|
||||
void SharedCmbBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
|
||||
{
|
||||
QComboBox *comboBox = static_cast<QComboBox *>(editor);
|
||||
|
||||
if (editor->focusPolicy() != Qt::ClickFocus) {
|
||||
return;
|
||||
}
|
||||
|
||||
editor->setFocusPolicy(Qt::StrongFocus);
|
||||
|
||||
QStringList assignedLayouts = index.model()->data(index, Qt::UserRole).toStringList();
|
||||
QString selectedLayout = comboBox->currentData().toString();
|
||||
|
||||
if (assignedLayouts.contains(selectedLayout)) {
|
||||
assignedLayouts.removeAll(selectedLayout);
|
||||
} else {
|
||||
assignedLayouts.append(selectedLayout);
|
||||
}
|
||||
|
||||
model->setData(index, assignedLayouts, Qt::UserRole);
|
||||
}
|
||||
|
||||
void SharedCmbBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||
{
|
||||
editor->setGeometry(option.rect);
|
||||
}
|
||||
|
||||
void SharedCmbBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||
{
|
||||
QStyleOptionViewItem myOptions = option;
|
||||
//! Remove the focus dotted lines
|
||||
myOptions.state = (myOptions.state & ~QStyle::State_HasFocus);
|
||||
painter->save();
|
||||
|
||||
QStringList assignedLayouts = index.model()->data(index, Qt::UserRole).toStringList();
|
||||
|
||||
if (assignedLayouts.count() > 0) {
|
||||
myOptions.text = assignedLayoutsText(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("<body>" + myOptions.text + "</body>");
|
||||
|
||||
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 SharedCmbBoxDelegate::assignedLayoutsText(const QModelIndex &index) const
|
||||
{
|
||||
QStringList assignedLayouts = index.model()->data(index, Qt::UserRole).toStringList();
|
||||
|
||||
QString finalText;
|
||||
|
||||
if (assignedLayouts.count() > 0) {
|
||||
for (int i = 0; i < assignedLayouts.count(); ++i) {
|
||||
if (i > 0) {
|
||||
finalText += ", ";
|
||||
}
|
||||
|
||||
finalText += assignedLayouts[i];
|
||||
}
|
||||
}
|
||||
|
||||
return finalText;
|
||||
}
|
53
app/settings/delegates/sharedcmbboxdelegate.h
Normal file
53
app/settings/delegates/sharedcmbboxdelegate.h
Normal file
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright 2019 Michail Vourlakos <mvourlakos@gmail.com>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef SHAREDCMBBOXDELEGATE_H
|
||||
#define SHAREDCMBBOXDELEGATE_H
|
||||
|
||||
// Qt
|
||||
#include <QItemDelegate>
|
||||
|
||||
class QModelIndex;
|
||||
class QWidget;
|
||||
class QVariant;
|
||||
|
||||
namespace Latte {
|
||||
class LayoutManager;
|
||||
class SettingsDialog;
|
||||
}
|
||||
|
||||
class SharedCmbBoxDelegate : public QItemDelegate
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
SharedCmbBoxDelegate(QObject *parent);
|
||||
|
||||
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
|
||||
void setEditorData(QWidget *editor, const QModelIndex &index) const;
|
||||
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
|
||||
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
|
||||
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
|
||||
|
||||
private:
|
||||
QString assignedLayoutsText(const QModelIndex &index) const;
|
||||
|
||||
Latte::SettingsDialog *m_settingsDialog{nullptr};
|
||||
};
|
||||
|
||||
#endif
|
@ -69,6 +69,7 @@ const int NAMECOLUMN = 3;
|
||||
const int MENUCOLUMN = 4;
|
||||
const int BORDERSCOLUMN = 5;
|
||||
const int ACTIVITYCOLUMN = 6;
|
||||
const int SHAREDCOLUMN = 7;
|
||||
|
||||
const int SCREENTRACKERDEFAULTVALUE = 2500;
|
||||
const int OUTLINEDEFAULTWIDTH = 1;
|
||||
|
Loading…
x
Reference in New Issue
Block a user