kcmkwin/kwinscreenedges: use more modern C++

This commit is contained in:
Xaver Hugl 2022-11-14 11:21:08 +01:00
parent 95599d2ffd
commit 0f3ae0216c
9 changed files with 107 additions and 148 deletions

View file

@ -7,9 +7,7 @@
SPDX-License-Identifier: GPL-2.0-or-later SPDX-License-Identifier: GPL-2.0-or-later
*/ */
#pragma once
#ifndef __KWINSCREENEDGE_H__
#define __KWINSCREENEDGE_H__
#include <QWidget> #include <QWidget>
@ -71,5 +69,3 @@ private:
}; };
} // namespace } // namespace
#endif

View file

@ -16,7 +16,7 @@ namespace KWin
KWinScreenEdgesConfigForm::KWinScreenEdgesConfigForm(QWidget *parent) KWinScreenEdgesConfigForm::KWinScreenEdgesConfigForm(QWidget *parent)
: KWinScreenEdge(parent) : KWinScreenEdge(parent)
, ui(new Ui::KWinScreenEdgesConfigUI) , ui(std::make_unique<Ui::KWinScreenEdgesConfigUI>())
{ {
ui->setupUi(this); ui->setupUi(this);
@ -32,10 +32,7 @@ KWinScreenEdgesConfigForm::KWinScreenEdgesConfigForm(QWidget *parent)
connect(ui->electricBorderCornerRatioSpin, qOverload<int>(&QSpinBox::valueChanged), this, &KWinScreenEdgesConfigForm::updateDefaultIndicators); connect(ui->electricBorderCornerRatioSpin, qOverload<int>(&QSpinBox::valueChanged), this, &KWinScreenEdgesConfigForm::updateDefaultIndicators);
} }
KWinScreenEdgesConfigForm::~KWinScreenEdgesConfigForm() KWinScreenEdgesConfigForm::~KWinScreenEdgesConfigForm() = default;
{
delete ui;
}
void KWinScreenEdgesConfigForm::setElectricBorderCornerRatio(double value) void KWinScreenEdgesConfigForm::setElectricBorderCornerRatio(double value)
{ {

View file

@ -66,7 +66,7 @@ private:
bool m_defaultIndicatorVisible = false; bool m_defaultIndicatorVisible = false;
Ui::KWinScreenEdgesConfigUI *ui; std::unique_ptr<Ui::KWinScreenEdgesConfigUI> ui;
}; };
} // namespace } // namespace

View file

@ -16,15 +16,12 @@ namespace KWin
KWinTouchScreenEdgeConfigForm::KWinTouchScreenEdgeConfigForm(QWidget *parent) KWinTouchScreenEdgeConfigForm::KWinTouchScreenEdgeConfigForm(QWidget *parent)
: KWinScreenEdge(parent) : KWinScreenEdge(parent)
, ui(new Ui::KWinTouchScreenConfigUi) , ui(std::make_unique<Ui::KWinTouchScreenConfigUi>())
{ {
ui->setupUi(this); ui->setupUi(this);
} }
KWinTouchScreenEdgeConfigForm::~KWinTouchScreenEdgeConfigForm() KWinTouchScreenEdgeConfigForm::~KWinTouchScreenEdgeConfigForm() = default;
{
delete ui;
}
Monitor *KWinTouchScreenEdgeConfigForm::monitor() const Monitor *KWinTouchScreenEdgeConfigForm::monitor() const
{ {

View file

@ -33,7 +33,7 @@ protected:
Monitor *monitor() const override; Monitor *monitor() const override;
private: private:
Ui::KWinTouchScreenConfigUi *ui; std::unique_ptr<Ui::KWinTouchScreenConfigUi> ui;
}; };
} // namespace } // namespace

View file

@ -38,41 +38,36 @@ static QScreen *screenFromWidget(const QWidget *widget)
Monitor::Monitor(QWidget *parent) Monitor::Monitor(QWidget *parent)
: ScreenPreviewWidget(parent) : ScreenPreviewWidget(parent)
{ {
for (int i = 0; for (auto &popup : m_popups) {
i < 8; popup = std::make_unique<QMenu>(this);
++i) {
popups[i] = new QMenu(this);
} }
scene = new QGraphicsScene(this); m_scene = std::make_unique<QGraphicsScene>(this);
view = new QGraphicsView(scene, this); m_view = std::make_unique<QGraphicsView>(m_scene.get(), this);
view->setBackgroundBrush(Qt::black); m_view->setBackgroundBrush(Qt::black);
view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view->setFocusPolicy(Qt::NoFocus); m_view->setFocusPolicy(Qt::NoFocus);
view->setFrameShape(QFrame::NoFrame); m_view->setFrameShape(QFrame::NoFrame);
for (int i = 0; for (size_t i = 0; i < m_items.size(); i++) {
i < 8; m_items[i] = std::make_unique<Corner>(this);
++i) { m_scene->addItem(m_items[i].get());
items[i] = new Corner(this); m_hidden[i] = false;
scene->addItem(items[i]); m_actionGroups[i] = std::make_unique<QActionGroup>(this);
hidden[i] = false;
grp[i] = new QActionGroup(this);
} }
QRect avail = screenFromWidget(this)->geometry(); QRect avail = screenFromWidget(this)->geometry();
setRatio((qreal)avail.width() / (qreal)avail.height()); setRatio((qreal)avail.width() / (qreal)avail.height());
checkSize(); checkSize();
} }
Monitor::~Monitor() = default;
void Monitor::clear() void Monitor::clear()
{ {
for (int i = 0; for (size_t i = 0; i < m_popups.size(); i++) {
i < 8; m_popups[i]->clear();
++i) { m_items[i]->setActive(false);
popups[i]->clear();
setEdge(i, false);
setEdgeHidden(i, false); setEdgeHidden(i, false);
delete grp[i]; m_actionGroups[i] = std::make_unique<QActionGroup>(this);
grp[i] = new QActionGroup(this);
} }
} }
@ -98,90 +93,80 @@ void Monitor::checkSize()
QRect contentsRect = previewRect(); QRect contentsRect = previewRect();
// int w = 151; // int w = 151;
// int h = 115; // int h = 115;
view->setGeometry(contentsRect); m_view->setGeometry(contentsRect);
scene->setSceneRect(QRect(QPoint(0, 0), contentsRect.size())); m_scene->setSceneRect(QRect(QPoint(0, 0), contentsRect.size()));
int x2 = (contentsRect.width() - 20) / 2; const int x2 = (contentsRect.width() - 20) / 2;
int x3 = contentsRect.width() - 20; const int x3 = contentsRect.width() - 20;
int y2 = (contentsRect.height() - 20) / 2; const int y2 = (contentsRect.height() - 20) / 2;
int y3 = contentsRect.height() - 20; const int y3 = contentsRect.height() - 20;
items[0]->setRect(0, y2, 20, 20); m_items[0]->setRect(0, y2, 20, 20);
items[1]->setRect(x3, y2, 20, 20); m_items[1]->setRect(x3, y2, 20, 20);
items[2]->setRect(x2, 0, 20, 20); m_items[2]->setRect(x2, 0, 20, 20);
items[3]->setRect(x2, y3, 20, 20); m_items[3]->setRect(x2, y3, 20, 20);
items[4]->setRect(0, 0, 20, 20); m_items[4]->setRect(0, 0, 20, 20);
items[5]->setRect(x3, 0, 20, 20); m_items[5]->setRect(x3, 0, 20, 20);
items[6]->setRect(0, y3, 20, 20); m_items[6]->setRect(0, y3, 20, 20);
items[7]->setRect(x3, y3, 20, 20); m_items[7]->setRect(x3, y3, 20, 20);
}
void Monitor::setEdge(int edge, bool set)
{
items[edge]->setActive(set);
}
bool Monitor::edge(int edge) const
{
return items[edge]->brush() == Qt::green;
} }
void Monitor::setEdgeEnabled(int edge, bool enabled) void Monitor::setEdgeEnabled(int edge, bool enabled)
{ {
for (QAction *action : std::as_const(popup_actions[edge])) { for (QAction *action : std::as_const(m_popupActions[edge])) {
action->setEnabled(enabled); action->setEnabled(enabled);
} }
} }
void Monitor::setEdgeHidden(int edge, bool set) void Monitor::setEdgeHidden(int edge, bool set)
{ {
hidden[edge] = set; m_hidden[edge] = set;
if (set) { if (set) {
items[edge]->hide(); m_items[edge]->hide();
} else { } else {
items[edge]->show(); m_items[edge]->show();
} }
} }
bool Monitor::edgeHidden(int edge) const bool Monitor::edgeHidden(int edge) const
{ {
return hidden[edge]; return m_hidden[edge];
} }
void Monitor::addEdgeItem(int edge, const QString &item) void Monitor::addEdgeItem(int edge, const QString &item)
{ {
QAction *act = popups[edge]->addAction(item); QAction *act = m_popups[edge]->addAction(item);
act->setCheckable(true); act->setCheckable(true);
popup_actions[edge].append(act); m_popupActions[edge].append(act);
grp[edge]->addAction(act); m_actionGroups[edge]->addAction(act);
if (popup_actions[edge].count() == 1) { if (m_popupActions[edge].count() == 1) {
act->setChecked(true); act->setChecked(true);
items[edge]->setToolTip(item); m_items[edge]->setToolTip(item);
} }
setEdge(edge, !popup_actions[edge][0]->isChecked()); m_items[edge]->setActive(!m_popupActions[edge].front()->isChecked());
} }
void Monitor::setEdgeItemEnabled(int edge, int index, bool enabled) void Monitor::setEdgeItemEnabled(int edge, int index, bool enabled)
{ {
popup_actions[edge][index]->setEnabled(enabled); m_popupActions[edge][index]->setEnabled(enabled);
} }
bool Monitor::edgeItemEnabled(int edge, int index) const bool Monitor::edgeItemEnabled(int edge, int index) const
{ {
return popup_actions[edge][index]->isEnabled(); return m_popupActions[edge][index]->isEnabled();
} }
void Monitor::selectEdgeItem(int edge, int index) void Monitor::selectEdgeItem(int edge, int index)
{ {
popup_actions[edge][index]->setChecked(true); m_popupActions[edge][index]->setChecked(true);
setEdge(edge, !popup_actions[edge][0]->isChecked()); m_items[edge]->setActive(!m_popupActions[edge].front()->isChecked());
QString actionText = popup_actions[edge][index]->text(); QString actionText = m_popupActions[edge][index]->text();
// remove accelerators added by KAcceleratorManager // remove accelerators added by KAcceleratorManager
actionText = KLocalizedString::removeAcceleratorMarker(actionText); actionText = KLocalizedString::removeAcceleratorMarker(actionText);
items[edge]->setToolTip(actionText); m_items[edge]->setToolTip(actionText);
} }
int Monitor::selectedEdgeItem(int edge) const int Monitor::selectedEdgeItem(int edge) const
{ {
const auto actions = popup_actions[edge]; const auto &actions = m_popupActions[edge];
for (QAction *act : actions) { for (QAction *act : actions) {
if (act->isChecked()) { if (act->isChecked()) {
return actions.indexOf(act); return actions.indexOf(act);
@ -192,17 +177,15 @@ int Monitor::selectedEdgeItem(int edge) const
void Monitor::popup(Corner *c, QPoint pos) void Monitor::popup(Corner *c, QPoint pos)
{ {
for (int i = 0; for (size_t i = 0; i < m_items.size(); i++) {
i < 8; if (m_items[i].get() == c) {
++i) { if (m_popupActions[i].empty()) {
if (items[i] == c) {
if (popup_actions[i].count() == 0) {
return; return;
} }
if (QAction *a = popups[i]->exec(pos)) { if (QAction *a = m_popups[i]->exec(pos)) {
selectEdgeItem(i, popup_actions[i].indexOf(a)); selectEdgeItem(i, m_popupActions[i].indexOf(a));
Q_EMIT changed(); Q_EMIT changed();
Q_EMIT edgeSelectionChanged(i, popup_actions[i].indexOf(a)); Q_EMIT edgeSelectionChanged(i, m_popupActions[i].indexOf(a));
c->setToolTip(KLocalizedString::removeAcceleratorMarker(a->text())); c->setToolTip(KLocalizedString::removeAcceleratorMarker(a->text()));
} }
return; return;
@ -213,12 +196,10 @@ void Monitor::popup(Corner *c, QPoint pos)
void Monitor::flip(Corner *c, QPoint pos) void Monitor::flip(Corner *c, QPoint pos)
{ {
for (int i = 0; for (size_t i = 0; i < m_items.size(); i++) {
i < 8; if (m_items[i].get() == c) {
++i) { if (m_popupActions[i].empty()) {
if (items[i] == c) { m_items[i]->setActive(m_items[i]->brush() != Qt::green);
if (popup_actions[i].count() == 0) {
setEdge(i, !edge(i));
} else { } else {
popup(c, pos); popup(c, pos);
} }
@ -229,52 +210,47 @@ void Monitor::flip(Corner *c, QPoint pos)
} }
Monitor::Corner::Corner(Monitor *m) Monitor::Corner::Corner(Monitor *m)
: monitor(m) : m_monitor(m)
, m_active(false) , m_button(std::make_unique<Plasma::FrameSvg>())
, m_hover(false)
{ {
button = new Plasma::FrameSvg(); m_button->setImagePath("widgets/button");
button->setImagePath("widgets/button");
setAcceptHoverEvents(true); setAcceptHoverEvents(true);
} }
Monitor::Corner::~Corner() Monitor::Corner::~Corner() = default;
{
delete button;
}
void Monitor::Corner::contextMenuEvent(QGraphicsSceneContextMenuEvent *e) void Monitor::Corner::contextMenuEvent(QGraphicsSceneContextMenuEvent *e)
{ {
monitor->popup(this, e->screenPos()); m_monitor->popup(this, e->screenPos());
} }
void Monitor::Corner::mousePressEvent(QGraphicsSceneMouseEvent *e) void Monitor::Corner::mousePressEvent(QGraphicsSceneMouseEvent *e)
{ {
monitor->flip(this, e->screenPos()); m_monitor->flip(this, e->screenPos());
} }
void Monitor::Corner::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) void Monitor::Corner::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{ {
if (m_hover) { if (m_hover) {
button->setElementPrefix("normal"); m_button->setElementPrefix("normal");
qreal left, top, right, bottom; qreal left, top, right, bottom;
button->getMargins(left, top, right, bottom); m_button->getMargins(left, top, right, bottom);
button->setElementPrefix("active"); m_button->setElementPrefix("active");
qreal activeLeft, activeTop, activeRight, activeBottom; qreal activeLeft, activeTop, activeRight, activeBottom;
button->getMargins(activeLeft, activeTop, activeRight, activeBottom); m_button->getMargins(activeLeft, activeTop, activeRight, activeBottom);
QRectF activeRect = QRectF(QPointF(0, 0), rect().size()); QRectF activeRect = QRectF(QPointF(0, 0), rect().size());
activeRect.adjust(left - activeLeft, top - activeTop, activeRect.adjust(left - activeLeft, top - activeTop,
-(right - activeRight), -(bottom - activeBottom)); -(right - activeRight), -(bottom - activeBottom));
button->setElementPrefix("active"); m_button->setElementPrefix("active");
button->resizeFrame(activeRect.size()); m_button->resizeFrame(activeRect.size());
button->paintFrame(painter, rect().topLeft() + activeRect.topLeft()); m_button->paintFrame(painter, rect().topLeft() + activeRect.topLeft());
} else { } else {
button->setElementPrefix(m_active ? "pressed" : "normal"); m_button->setElementPrefix(m_active ? "pressed" : "normal");
button->resizeFrame(rect().size()); m_button->resizeFrame(rect().size());
button->paintFrame(painter, rect().topLeft()); m_button->paintFrame(painter, rect().topLeft());
} }
if (m_active) { if (m_active) {

View file

@ -16,6 +16,8 @@
#include <QActionGroup> #include <QActionGroup>
#include <QGraphicsItem> #include <QGraphicsItem>
#include <QVector> #include <QVector>
#include <array>
#include <memory>
class QAction; class QAction;
class QGraphicsView; class QGraphicsView;
@ -30,14 +32,13 @@ class FrameSvg;
namespace KWin namespace KWin
{ {
class Monitor class Monitor : public ScreenPreviewWidget
: public ScreenPreviewWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit Monitor(QWidget *parent); explicit Monitor(QWidget *parent);
void setEdge(int edge, bool set); ~Monitor();
bool edge(int edge) const;
void setEdgeEnabled(int edge, bool enabled); void setEdgeEnabled(int edge, bool enabled);
void setEdgeHidden(int edge, bool set); void setEdgeHidden(int edge, bool set);
bool edgeHidden(int edge) const; bool edgeHidden(int edge) const;
@ -72,17 +73,16 @@ private:
void popup(Corner *c, QPoint pos); void popup(Corner *c, QPoint pos);
void flip(Corner *c, QPoint pos); void flip(Corner *c, QPoint pos);
void checkSize(); void checkSize();
QGraphicsView *view; std::unique_ptr<QGraphicsScene> m_scene;
QGraphicsScene *scene; std::unique_ptr<QGraphicsView> m_view;
Corner *items[8]; std::array<std::unique_ptr<Corner>, 8> m_items;
bool hidden[8]; std::array<bool, 8> m_hidden;
QMenu *popups[8]; std::array<std::unique_ptr<QMenu>, 8> m_popups;
QVector<QAction *> popup_actions[8]; std::array<QVector<QAction *>, 8> m_popupActions;
QActionGroup *grp[8]; std::array<std::unique_ptr<QActionGroup>, 8> m_actionGroups;
}; };
class Monitor::Corner class Monitor::Corner : public QGraphicsRectItem
: public QGraphicsRectItem
{ {
public: public:
Corner(Monitor *m); Corner(Monitor *m);
@ -98,10 +98,10 @@ protected:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
private: private:
Monitor *monitor; Monitor *const m_monitor;
Plasma::FrameSvg *button; const std::unique_ptr<Plasma::FrameSvg> m_button;
bool m_active; bool m_active = false;
bool m_hover; bool m_hover = false;
}; };
} // namespace } // namespace

View file

@ -65,17 +65,14 @@ public:
ScreenPreviewWidget::ScreenPreviewWidget(QWidget *parent) ScreenPreviewWidget::ScreenPreviewWidget(QWidget *parent)
: QWidget(parent) : QWidget(parent)
, d(new ScreenPreviewWidgetPrivate(this)) , d(std::make_unique<ScreenPreviewWidgetPrivate>(this))
{ {
d->screenGraphics = new Plasma::FrameSvg(this); d->screenGraphics = new Plasma::FrameSvg(this);
d->screenGraphics->setImagePath("widgets/monitor"); d->screenGraphics->setImagePath("widgets/monitor");
d->updateScreenGraphics(); d->updateScreenGraphics();
} }
ScreenPreviewWidget::~ScreenPreviewWidget() ScreenPreviewWidget::~ScreenPreviewWidget() = default;
{
delete d;
}
void ScreenPreviewWidget::setPreview(const QPixmap &preview) void ScreenPreviewWidget::setPreview(const QPixmap &preview)
{ {

View file

@ -3,9 +3,7 @@
SPDX-License-Identifier: LGPL-2.0-or-later SPDX-License-Identifier: LGPL-2.0-or-later
*/ */
#pragma once
#ifndef SCREENPREVIEWWIDGET_H
#define SCREENPREVIEWWIDGET_H
#include <QWidget> #include <QWidget>
@ -35,9 +33,7 @@ Q_SIGNALS:
void imageDropped(const QString &); void imageDropped(const QString &);
private: private:
ScreenPreviewWidgetPrivate *const d; void updateRect(const QRectF &rect);
Q_PRIVATE_SLOT(d, void updateRect(const QRectF &rect)) const std::unique_ptr<ScreenPreviewWidgetPrivate> d;
}; };
#endif