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

0001 /*
0002     SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 #include "previewclient.h"
0007 #include <KDecoration2/DecoratedClient>
0008 #include <KDecoration2/Decoration>
0009 
0010 #include <QCoreApplication>
0011 #include <QDebug>
0012 #include <QEvent>
0013 #include <QModelIndex>
0014 
0015 namespace KDecoration2
0016 {
0017 namespace Preview
0018 {
0019 
0020 PreviewClient::PreviewClient(DecoratedClient *c, Decoration *decoration)
0021     : QObject(decoration)
0022     , ApplicationMenuEnabledDecoratedClientPrivate(c, decoration)
0023     , m_icon(QIcon::fromTheme(QStringLiteral("start-here-kde")))
0024     , m_iconName(m_icon.name())
0025     , m_palette(QStringLiteral("kdeglobals"))
0026     , m_active(true)
0027     , m_closeable(true)
0028     , m_keepBelow(false)
0029     , m_keepAbove(false)
0030     , m_maximizable(true)
0031     , m_maximizedHorizontally(false)
0032     , m_maximizedVertically(false)
0033     , m_minimizable(true)
0034     , m_modal(false)
0035     , m_movable(true)
0036     , m_resizable(true)
0037     , m_shadeable(true)
0038     , m_shaded(false)
0039     , m_providesContextHelp(false)
0040     , m_onAllDesktops(false)
0041     , m_width(0)
0042     , m_height(0)
0043     , m_bordersTopEdge(false)
0044     , m_bordersLeftEdge(false)
0045     , m_bordersRightEdge(false)
0046     , m_bordersBottomEdge(false)
0047 {
0048     connect(this, &PreviewClient::captionChanged, c, &DecoratedClient::captionChanged);
0049     connect(this, &PreviewClient::activeChanged, c, &DecoratedClient::activeChanged);
0050     connect(this, &PreviewClient::closeableChanged, c, &DecoratedClient::closeableChanged);
0051     connect(this, &PreviewClient::keepAboveChanged, c, &DecoratedClient::keepAboveChanged);
0052     connect(this, &PreviewClient::keepBelowChanged, c, &DecoratedClient::keepBelowChanged);
0053     connect(this, &PreviewClient::maximizableChanged, c, &DecoratedClient::maximizeableChanged);
0054     connect(this, &PreviewClient::maximizedChanged, c, &DecoratedClient::maximizedChanged);
0055     connect(this, &PreviewClient::maximizedVerticallyChanged, c, &DecoratedClient::maximizedVerticallyChanged);
0056     connect(this, &PreviewClient::maximizedHorizontallyChanged, c, &DecoratedClient::maximizedHorizontallyChanged);
0057     connect(this, &PreviewClient::minimizableChanged, c, &DecoratedClient::minimizeableChanged);
0058     connect(this, &PreviewClient::movableChanged, c, &DecoratedClient::moveableChanged);
0059     connect(this, &PreviewClient::onAllDesktopsChanged, c, &DecoratedClient::onAllDesktopsChanged);
0060     connect(this, &PreviewClient::resizableChanged, c, &DecoratedClient::resizeableChanged);
0061     connect(this, &PreviewClient::shadeableChanged, c, &DecoratedClient::shadeableChanged);
0062     connect(this, &PreviewClient::shadedChanged, c, &DecoratedClient::shadedChanged);
0063     connect(this, &PreviewClient::providesContextHelpChanged, c, &DecoratedClient::providesContextHelpChanged);
0064     connect(this, &PreviewClient::widthChanged, c, &DecoratedClient::widthChanged);
0065     connect(this, &PreviewClient::heightChanged, c, &DecoratedClient::heightChanged);
0066     connect(this, &PreviewClient::iconChanged, c, &DecoratedClient::iconChanged);
0067     connect(this, &PreviewClient::paletteChanged, c, &DecoratedClient::paletteChanged);
0068     connect(this, &PreviewClient::maximizedVerticallyChanged, this, [this]() {
0069         Q_EMIT maximizedChanged(isMaximized());
0070     });
0071     connect(this, &PreviewClient::maximizedHorizontallyChanged, this, [this]() {
0072         Q_EMIT maximizedChanged(isMaximized());
0073     });
0074     connect(this, &PreviewClient::iconNameChanged, this, [this]() {
0075         m_icon = QIcon::fromTheme(m_iconName);
0076         Q_EMIT iconChanged(m_icon);
0077     });
0078     connect(&m_palette, &KWin::Decoration::DecorationPalette::changed, this, [this]() {
0079         Q_EMIT paletteChanged(m_palette.palette());
0080     });
0081     auto emitEdgesChanged = [this, c]() {
0082         Q_EMIT c->adjacentScreenEdgesChanged(adjacentScreenEdges());
0083     };
0084     connect(this, &PreviewClient::bordersTopEdgeChanged, this, emitEdgesChanged);
0085     connect(this, &PreviewClient::bordersLeftEdgeChanged, this, emitEdgesChanged);
0086     connect(this, &PreviewClient::bordersRightEdgeChanged, this, emitEdgesChanged);
0087     connect(this, &PreviewClient::bordersBottomEdgeChanged, this, emitEdgesChanged);
0088     auto emitSizeChanged = [c]() {
0089         Q_EMIT c->sizeChanged(c->size());
0090     };
0091     connect(this, &PreviewClient::widthChanged, this, emitSizeChanged);
0092     connect(this, &PreviewClient::heightChanged, this, emitSizeChanged);
0093 
0094     qApp->installEventFilter(this);
0095 }
0096 
0097 PreviewClient::~PreviewClient() = default;
0098 
0099 void PreviewClient::setIcon(const QIcon &pixmap)
0100 {
0101     m_icon = pixmap;
0102     Q_EMIT iconChanged(m_icon);
0103 }
0104 
0105 int PreviewClient::width() const
0106 {
0107     return m_width;
0108 }
0109 
0110 int PreviewClient::height() const
0111 {
0112     return m_height;
0113 }
0114 
0115 QSize PreviewClient::size() const
0116 {
0117     return QSize(m_width, m_height);
0118 }
0119 
0120 QString PreviewClient::caption() const
0121 {
0122     return m_caption;
0123 }
0124 
0125 WId PreviewClient::decorationId() const
0126 {
0127     return 0;
0128 }
0129 
0130 QIcon PreviewClient::icon() const
0131 {
0132     return m_icon;
0133 }
0134 
0135 QString PreviewClient::iconName() const
0136 {
0137     return m_iconName;
0138 }
0139 
0140 bool PreviewClient::isActive() const
0141 {
0142     return m_active;
0143 }
0144 
0145 bool PreviewClient::isCloseable() const
0146 {
0147     return m_closeable;
0148 }
0149 
0150 bool PreviewClient::isKeepAbove() const
0151 {
0152     return m_keepAbove;
0153 }
0154 
0155 bool PreviewClient::isKeepBelow() const
0156 {
0157     return m_keepBelow;
0158 }
0159 
0160 bool PreviewClient::isMaximizeable() const
0161 {
0162     return m_maximizable;
0163 }
0164 
0165 bool PreviewClient::isMaximized() const
0166 {
0167     return isMaximizedHorizontally() && isMaximizedVertically();
0168 }
0169 
0170 bool PreviewClient::isMaximizedHorizontally() const
0171 {
0172     return m_maximizedHorizontally;
0173 }
0174 
0175 bool PreviewClient::isMaximizedVertically() const
0176 {
0177     return m_maximizedVertically;
0178 }
0179 
0180 bool PreviewClient::isMinimizeable() const
0181 {
0182     return m_minimizable;
0183 }
0184 
0185 bool PreviewClient::isModal() const
0186 {
0187     return m_modal;
0188 }
0189 
0190 bool PreviewClient::isMoveable() const
0191 {
0192     return m_movable;
0193 }
0194 
0195 bool PreviewClient::isOnAllDesktops() const
0196 {
0197     return m_onAllDesktops;
0198 }
0199 
0200 bool PreviewClient::isResizeable() const
0201 {
0202     return m_resizable;
0203 }
0204 
0205 bool PreviewClient::isShadeable() const
0206 {
0207     return m_shadeable;
0208 }
0209 
0210 bool PreviewClient::isShaded() const
0211 {
0212     return m_shaded;
0213 }
0214 
0215 bool PreviewClient::providesContextHelp() const
0216 {
0217     return m_providesContextHelp;
0218 }
0219 
0220 WId PreviewClient::windowId() const
0221 {
0222     return 0;
0223 }
0224 
0225 QPalette PreviewClient::palette() const
0226 {
0227     return m_palette.palette();
0228 }
0229 
0230 QColor PreviewClient::color(ColorGroup group, ColorRole role) const
0231 {
0232     return m_palette.color(group, role);
0233 }
0234 
0235 Qt::Edges PreviewClient::adjacentScreenEdges() const
0236 {
0237     Qt::Edges edges;
0238     if (m_bordersBottomEdge) {
0239         edges |= Qt::BottomEdge;
0240     }
0241     if (m_bordersLeftEdge) {
0242         edges |= Qt::LeftEdge;
0243     }
0244     if (m_bordersRightEdge) {
0245         edges |= Qt::RightEdge;
0246     }
0247     if (m_bordersTopEdge) {
0248         edges |= Qt::TopEdge;
0249     }
0250     return edges;
0251 }
0252 
0253 QString PreviewClient::windowClass() const
0254 {
0255     return QString();
0256 }
0257 
0258 bool PreviewClient::hasApplicationMenu() const
0259 {
0260     return true;
0261 }
0262 
0263 bool PreviewClient::isApplicationMenuActive() const
0264 {
0265     return false;
0266 }
0267 
0268 bool PreviewClient::bordersBottomEdge() const
0269 {
0270     return m_bordersBottomEdge;
0271 }
0272 
0273 bool PreviewClient::bordersLeftEdge() const
0274 {
0275     return m_bordersLeftEdge;
0276 }
0277 
0278 bool PreviewClient::bordersRightEdge() const
0279 {
0280     return m_bordersRightEdge;
0281 }
0282 
0283 bool PreviewClient::bordersTopEdge() const
0284 {
0285     return m_bordersTopEdge;
0286 }
0287 
0288 void PreviewClient::setBordersBottomEdge(bool enabled)
0289 {
0290     if (m_bordersBottomEdge == enabled) {
0291         return;
0292     }
0293     m_bordersBottomEdge = enabled;
0294     Q_EMIT bordersBottomEdgeChanged(enabled);
0295 }
0296 
0297 void PreviewClient::setBordersLeftEdge(bool enabled)
0298 {
0299     if (m_bordersLeftEdge == enabled) {
0300         return;
0301     }
0302     m_bordersLeftEdge = enabled;
0303     Q_EMIT bordersLeftEdgeChanged(enabled);
0304 }
0305 
0306 void PreviewClient::setBordersRightEdge(bool enabled)
0307 {
0308     if (m_bordersRightEdge == enabled) {
0309         return;
0310     }
0311     m_bordersRightEdge = enabled;
0312     Q_EMIT bordersRightEdgeChanged(enabled);
0313 }
0314 
0315 void PreviewClient::setBordersTopEdge(bool enabled)
0316 {
0317     if (m_bordersTopEdge == enabled) {
0318         return;
0319     }
0320     m_bordersTopEdge = enabled;
0321     Q_EMIT bordersTopEdgeChanged(enabled);
0322 }
0323 
0324 void PreviewClient::requestShowToolTip(const QString &text)
0325 {
0326 }
0327 
0328 void PreviewClient::requestHideToolTip()
0329 {
0330 }
0331 
0332 void PreviewClient::requestClose()
0333 {
0334     Q_EMIT closeRequested();
0335 }
0336 
0337 void PreviewClient::requestContextHelp()
0338 {
0339 }
0340 
0341 void PreviewClient::requestToggleMaximization(Qt::MouseButtons buttons)
0342 {
0343     if (buttons.testFlag(Qt::LeftButton)) {
0344         const bool set = !isMaximized();
0345         setMaximizedHorizontally(set);
0346         setMaximizedVertically(set);
0347     } else if (buttons.testFlag(Qt::RightButton)) {
0348         setMaximizedHorizontally(!isMaximizedHorizontally());
0349     } else if (buttons.testFlag(Qt::MiddleButton)) {
0350         setMaximizedVertically(!isMaximizedVertically());
0351     }
0352 }
0353 
0354 void PreviewClient::requestMinimize()
0355 {
0356     Q_EMIT minimizeRequested();
0357 }
0358 
0359 void PreviewClient::requestToggleKeepAbove()
0360 {
0361     setKeepAbove(!isKeepAbove());
0362 }
0363 
0364 void PreviewClient::requestToggleKeepBelow()
0365 {
0366     setKeepBelow(!isKeepBelow());
0367 }
0368 
0369 void PreviewClient::requestShowWindowMenu(const QRect &rect)
0370 {
0371     Q_EMIT showWindowMenuRequested();
0372 }
0373 
0374 void PreviewClient::requestShowApplicationMenu(const QRect &rect, int actionId)
0375 {
0376 }
0377 
0378 void PreviewClient::showApplicationMenu(int actionId)
0379 {
0380 }
0381 
0382 void PreviewClient::requestToggleOnAllDesktops()
0383 {
0384     m_onAllDesktops = !m_onAllDesktops;
0385     Q_EMIT onAllDesktopsChanged(m_onAllDesktops);
0386 }
0387 
0388 void PreviewClient::requestToggleShade()
0389 {
0390     setShaded(!isShaded());
0391 }
0392 
0393 #define SETTER(type, name, variable)            \
0394     void PreviewClient::name(type variable)     \
0395     {                                           \
0396         if (m_##variable == variable) {         \
0397             return;                             \
0398         }                                       \
0399         m_##variable = variable;                \
0400         Q_EMIT variable##Changed(m_##variable); \
0401     }
0402 
0403 #define SETTER2(name, variable) SETTER(bool, name, variable)
0404 
0405 SETTER(const QString &, setCaption, caption)
0406 SETTER(const QString &, setIconName, iconName)
0407 SETTER(int, setWidth, width)
0408 SETTER(int, setHeight, height)
0409 
0410 SETTER2(setActive, active)
0411 SETTER2(setCloseable, closeable)
0412 SETTER2(setMaximizable, maximizable)
0413 SETTER2(setKeepBelow, keepBelow)
0414 SETTER2(setKeepAbove, keepAbove)
0415 SETTER2(setMaximizedHorizontally, maximizedHorizontally)
0416 SETTER2(setMaximizedVertically, maximizedVertically)
0417 SETTER2(setMinimizable, minimizable)
0418 SETTER2(setModal, modal)
0419 SETTER2(setMovable, movable)
0420 SETTER2(setResizable, resizable)
0421 SETTER2(setShadeable, shadeable)
0422 SETTER2(setShaded, shaded)
0423 SETTER2(setProvidesContextHelp, providesContextHelp)
0424 
0425 #undef SETTER2
0426 #undef SETTER
0427 
0428 } // namespace Preview
0429 } // namespace KDecoration2
0430 
0431 #include "moc_previewclient.cpp"