1
0
mirror of https://github.com/KDE/latte-dock.git synced 2025-03-30 14:50:12 +03:00

introduce new menu for SharedToLayout

This commit is contained in:
Michail Vourlakos 2019-05-05 11:19:57 +03:00
parent 1090e6a22d
commit 93ede645b8
7 changed files with 161 additions and 81 deletions

View File

@ -5,6 +5,7 @@ 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
${CMAKE_CURRENT_SOURCE_DIR}/shareddelegate.cpp
${CMAKE_CURRENT_SOURCE_DIR}/persistentmenu.cpp
PARENT_SCOPE
)

View File

@ -0,0 +1,47 @@
/*
* 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 "persistentmenu.h"
PersistentMenu::PersistentMenu(QWidget *parent = 0)
: QMenu (parent),
m_blockHide(false)
{
}
void PersistentMenu::setVisible (bool visible)
{
if (m_blockHide) {
m_blockHide = false;
return;
}
QMenu::setVisible (visible);
}
void PersistentMenu::mouseReleaseEvent (QMouseEvent *e)
{
const QAction *action = actionAt (e->pos ());
if (action) {
m_blockHide = true;
}
QMenu::mouseReleaseEvent (e);
}

View File

@ -0,0 +1,42 @@
/*
* 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 PERSISTENTMENU_H
#define PERSISTENTMENU_H
//Qt
#include <QMenu>
#include <QMouseEvent>
class PersistentMenu : public QMenu
{
Q_OBJECT
public:
PersistentMenu(QWidget *parent);
protected:
void setVisible (bool visible) override;
void mouseReleaseEvent (QMouseEvent *e) override;
private:
bool m_blockHide{false};
};
#endif

View File

@ -17,25 +17,27 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "sharedcmbboxdelegate.h"
#include "shareddelegate.h"
// local
#include "persistentmenu.h"
#include "../settingsdialog.h"
// Qt
#include <QAction>
#include <QApplication>
#include <QComboBox>
#include <QDebug>
#include <QMenu>
#include <QWidget>
#include <QModelIndex>
#include <QPainter>
#include <QPushButton>
#include <QString>
#include <QTextDocument>
// KDE
#include <KActivities/Info>
SharedCmbBoxDelegate::SharedCmbBoxDelegate(QObject *parent)
SharedDelegate::SharedDelegate(QObject *parent)
: QItemDelegate(parent)
{
auto *settingsDialog = qobject_cast<Latte::SettingsDialog *>(parent);
@ -45,78 +47,58 @@ SharedCmbBoxDelegate::SharedCmbBoxDelegate(QObject *parent)
}
}
QWidget *SharedCmbBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
QWidget *SharedDelegate::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 assignedShares = index.model()->data(index, Qt::UserRole).toStringList();
QStringList availableShares = m_settingsDialog->availableSharesFor(index.row());
QPushButton *button = new QPushButton(parent);
PersistentMenu *menu = new PersistentMenu(button);
button->setMenu(menu);
menu->setMinimumWidth(option.rect.width());
for (unsigned int i = 0; i < availableShares.count(); ++i) {
QString indicator = " ";
QAction *action = new QAction(availableShares[i]);
action->setCheckable(true);
action->setChecked(assignedShares.contains(availableShares[i]));
menu->addAction(action);
if (assignedShares.contains(availableShares[i])) {
indicator = QString::fromUtf8("\u2714") + " ";
connect(action, &QAction::toggled, this, [this, button]() {
updateButtonText(button);
});
}
updateButtonText(button);
return button;
}
void SharedDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
updateButtonText(editor);
}
void SharedDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QPushButton *button = static_cast<QPushButton *>(editor);
QStringList assignedLayouts;
foreach (QAction *action, button->menu()->actions()) {
if (action->isChecked()) {
assignedLayouts << action->text().replace("&","");
}
editor->addItem(QString(indicator + availableShares[i]), QVariant(availableShares[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
void SharedDelegate::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
void SharedDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem myOptions = option;
//! Remove the focus dotted lines
@ -126,7 +108,7 @@ void SharedCmbBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &
QStringList assignedLayouts = index.model()->data(index, Qt::UserRole).toStringList();
if (assignedLayouts.count() > 0) {
myOptions.text = assignedLayoutsText(index);
myOptions.text = joined(assignedLayouts);
QTextDocument doc;
QString css;
@ -168,21 +150,24 @@ void SharedCmbBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &
painter->restore();
}
QString SharedCmbBoxDelegate::assignedLayoutsText(const QModelIndex &index) const
void SharedDelegate::updateButtonText(QWidget *editor) const
{
QStringList assignedLayouts = index.model()->data(index, Qt::UserRole).toStringList();
if (!editor) {
return;
}
QPushButton *button = static_cast<QPushButton *>(editor);
QStringList assignedLayouts;
QString finalText;
if (assignedLayouts.count() > 0) {
for (int i = 0; i < assignedLayouts.count(); ++i) {
if (i > 0) {
finalText += ", ";
}
finalText += assignedLayouts[i];
foreach (QAction *action, button->menu()->actions()) {
if (action->isChecked()) {
assignedLayouts << action->text().replace("&","");
}
}
return finalText;
button->setText(joined(assignedLayouts));
}
QString SharedDelegate::joined(const QStringList &texts) const
{
return texts.join(", ");
}

View File

@ -17,12 +17,13 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SHAREDCMBBOXDELEGATE_H
#define SHAREDCMBBOXDELEGATE_H
#ifndef SHAREDDELEGATE_H
#define SHAREDDELEGATE_H
// Qt
#include <QItemDelegate>
class QMenu;
class QModelIndex;
class QWidget;
class QVariant;
@ -32,11 +33,11 @@ class LayoutManager;
class SettingsDialog;
}
class SharedCmbBoxDelegate : public QItemDelegate
class SharedDelegate : public QItemDelegate
{
Q_OBJECT
public:
SharedCmbBoxDelegate(QObject *parent);
SharedDelegate(QObject *parent);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
@ -45,9 +46,13 @@ public:
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
private:
QString assignedLayoutsText(const QModelIndex &index) const;
void updateButtonText(QWidget *editor) const;
QString joined(const QStringList &texts) const;
private:
Latte::SettingsDialog *m_settingsDialog{nullptr};
};
#endif

View File

@ -35,7 +35,7 @@
#include "delegates/colorcmbboxdelegate.h"
#include "delegates/activitycmbboxdelegate.h"
#include "delegates/layoutnamedelegate.h"
#include "delegates/sharedcmbboxdelegate.h"
#include "delegates/shareddelegate.h"
// Qt
#include <QButtonGroup>
@ -122,7 +122,7 @@ SettingsDialog::SettingsDialog(QWidget *parent, Latte::Corona *corona)
ui->layoutsView->setItemDelegateForColumn(MENUCOLUMN, new CheckBoxDelegate(this));
ui->layoutsView->setItemDelegateForColumn(BORDERSCOLUMN, new CheckBoxDelegate(this));
ui->layoutsView->setItemDelegateForColumn(ACTIVITYCOLUMN, new ActivityCmbBoxDelegate(this));
ui->layoutsView->setItemDelegateForColumn(SHAREDCOLUMN, new SharedCmbBoxDelegate(this));
ui->layoutsView->setItemDelegateForColumn(SHAREDCOLUMN, new SharedDelegate(this));
m_inMemoryButtons = new QButtonGroup(this);
m_inMemoryButtons->addButton(ui->singleToolBtn, Latte::Types::SingleLayout);

0
persistentmenu.cpp Normal file
View File