File indexing completed on 2024-11-10 04:56:50

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2009 Lucas Murray <lmurray@undefinedfire.com>
0006     SPDX-FileCopyrightText: 2020 Cyril Rossi <cyril.rossi@enioka.com>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #include "kwinscreenedge.h"
0012 
0013 #include "monitor.h"
0014 
0015 namespace KWin
0016 {
0017 
0018 KWinScreenEdge::KWinScreenEdge(QWidget *parent)
0019     : QWidget(parent)
0020 {
0021     QMetaObject::invokeMethod(this, &KWinScreenEdge::createConnection, Qt::QueuedConnection);
0022 }
0023 
0024 KWinScreenEdge::~KWinScreenEdge()
0025 {
0026 }
0027 
0028 void KWinScreenEdge::monitorHideEdge(ElectricBorder border, bool hidden)
0029 {
0030     const int edge = KWinScreenEdge::electricBorderToMonitorEdge(border);
0031     monitor()->setEdgeHidden(edge, hidden);
0032 }
0033 
0034 void KWinScreenEdge::monitorEnableEdge(ElectricBorder border, bool enabled)
0035 {
0036     const int edge = KWinScreenEdge::electricBorderToMonitorEdge(border);
0037     monitor()->setEdgeEnabled(edge, enabled);
0038 }
0039 
0040 void KWinScreenEdge::monitorAddItem(const QString &item)
0041 {
0042     for (int i = 0; i < 8; i++) {
0043         monitor()->addEdgeItem(i, item);
0044     }
0045 }
0046 
0047 void KWinScreenEdge::monitorItemSetEnabled(int index, bool enabled)
0048 {
0049     for (int i = 0; i < 8; i++) {
0050         monitor()->setEdgeItemEnabled(i, index, enabled);
0051     }
0052 }
0053 
0054 void KWinScreenEdge::monitorChangeEdge(const QList<int> &borderList, int index)
0055 {
0056     for (int border : borderList) {
0057         monitorChangeEdge(static_cast<ElectricBorder>(border), index);
0058     }
0059 }
0060 
0061 void KWinScreenEdge::monitorChangeEdge(ElectricBorder border, int index)
0062 {
0063     if (ELECTRIC_COUNT == border || ElectricNone == border) {
0064         return;
0065     }
0066     m_reference[border] = index;
0067     monitor()->selectEdgeItem(KWinScreenEdge::electricBorderToMonitorEdge(border), index);
0068 }
0069 
0070 QList<int> KWinScreenEdge::monitorCheckEffectHasEdge(int index) const
0071 {
0072     QList<int> list;
0073     if (monitor()->selectedEdgeItem(Monitor::Top) == index) {
0074         list.append(ElectricTop);
0075     }
0076     if (monitor()->selectedEdgeItem(Monitor::TopRight) == index) {
0077         list.append(ElectricTopRight);
0078     }
0079     if (monitor()->selectedEdgeItem(Monitor::Right) == index) {
0080         list.append(ElectricRight);
0081     }
0082     if (monitor()->selectedEdgeItem(Monitor::BottomRight) == index) {
0083         list.append(ElectricBottomRight);
0084     }
0085     if (monitor()->selectedEdgeItem(Monitor::Bottom) == index) {
0086         list.append(ElectricBottom);
0087     }
0088     if (monitor()->selectedEdgeItem(Monitor::BottomLeft) == index) {
0089         list.append(ElectricBottomLeft);
0090     }
0091     if (monitor()->selectedEdgeItem(Monitor::Left) == index) {
0092         list.append(ElectricLeft);
0093     }
0094     if (monitor()->selectedEdgeItem(Monitor::TopLeft) == index) {
0095         list.append(ElectricTopLeft);
0096     }
0097 
0098     if (list.isEmpty()) {
0099         list.append(ElectricNone);
0100     }
0101     return list;
0102 }
0103 
0104 int KWinScreenEdge::selectedEdgeItem(ElectricBorder border) const
0105 {
0106     return monitor()->selectedEdgeItem(KWinScreenEdge::electricBorderToMonitorEdge(border));
0107 }
0108 
0109 void KWinScreenEdge::monitorChangeDefaultEdge(ElectricBorder border, int index)
0110 {
0111     if (ELECTRIC_COUNT == border || ElectricNone == border) {
0112         return;
0113     }
0114     m_default[border] = index;
0115 }
0116 
0117 void KWinScreenEdge::monitorChangeDefaultEdge(const QList<int> &borderList, int index)
0118 {
0119     for (int border : borderList) {
0120         monitorChangeDefaultEdge(static_cast<ElectricBorder>(border), index);
0121     }
0122 }
0123 
0124 void KWinScreenEdge::reload()
0125 {
0126     for (auto it = m_reference.cbegin(); it != m_reference.cend(); ++it) {
0127         monitor()->selectEdgeItem(KWinScreenEdge::electricBorderToMonitorEdge(it.key()), it.value());
0128     }
0129     onChanged();
0130 }
0131 
0132 void KWinScreenEdge::setDefaults()
0133 {
0134     for (auto it = m_default.cbegin(); it != m_default.cend(); ++it) {
0135         monitor()->selectEdgeItem(KWinScreenEdge::electricBorderToMonitorEdge(it.key()), it.value());
0136     }
0137     onChanged();
0138 }
0139 
0140 int KWinScreenEdge::electricBorderToMonitorEdge(ElectricBorder border)
0141 {
0142     switch (border) {
0143     case ElectricTop:
0144         return Monitor::Top;
0145     case ElectricTopRight:
0146         return Monitor::TopRight;
0147     case ElectricRight:
0148         return Monitor::Right;
0149     case ElectricBottomRight:
0150         return Monitor::BottomRight;
0151     case ElectricBottom:
0152         return Monitor::Bottom;
0153     case ElectricBottomLeft:
0154         return Monitor::BottomLeft;
0155     case ElectricLeft:
0156         return Monitor::Left;
0157     case ElectricTopLeft:
0158         return Monitor::TopLeft;
0159     default: // ELECTRIC_COUNT and ElectricNone
0160         return Monitor::None;
0161     }
0162 }
0163 
0164 ElectricBorder KWinScreenEdge::monitorEdgeToElectricBorder(int edge)
0165 {
0166     const Monitor::Edges monitorEdge = static_cast<Monitor::Edges>(edge);
0167     switch (monitorEdge) {
0168     case Monitor::Left:
0169         return ElectricLeft;
0170     case Monitor::Right:
0171         return ElectricRight;
0172     case Monitor::Top:
0173         return ElectricTop;
0174     case Monitor::Bottom:
0175         return ElectricBottom;
0176     case Monitor::TopLeft:
0177         return ElectricTopLeft;
0178     case Monitor::TopRight:
0179         return ElectricTopRight;
0180     case Monitor::BottomLeft:
0181         return ElectricBottomLeft;
0182     case Monitor::BottomRight:
0183         return ElectricBottomRight;
0184     default:
0185         return ElectricNone;
0186     }
0187 }
0188 
0189 void KWinScreenEdge::onChanged()
0190 {
0191     bool needSave = isSaveNeeded();
0192     for (auto it = m_reference.cbegin(); it != m_reference.cend(); ++it) {
0193         needSave |= it.value() != monitor()->selectedEdgeItem(KWinScreenEdge::electricBorderToMonitorEdge(it.key()));
0194     }
0195     Q_EMIT saveNeededChanged(needSave);
0196 
0197     bool defaults = isDefault();
0198     for (auto it = m_default.cbegin(); it != m_default.cend(); ++it) {
0199         defaults &= it.value() == monitor()->selectedEdgeItem(KWinScreenEdge::electricBorderToMonitorEdge(it.key()));
0200     }
0201     Q_EMIT defaultChanged(defaults);
0202 }
0203 
0204 void KWinScreenEdge::createConnection()
0205 {
0206     connect(monitor(),
0207             &Monitor::changed,
0208             this,
0209             &KWinScreenEdge::onChanged);
0210 }
0211 
0212 bool KWinScreenEdge::isSaveNeeded() const
0213 {
0214     return false;
0215 }
0216 
0217 bool KWinScreenEdge::isDefault() const
0218 {
0219     return true;
0220 }
0221 
0222 } // namespace
0223 
0224 #include "moc_kwinscreenedge.cpp"