2008-11-06 10:42:36 +00:00
|
|
|
/********************************************************************
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
|
|
|
|
|
|
|
Copyright (C) 2008 Lubos Lunak <l.lunak@suse.cz>
|
2009-02-27 15:40:53 +00:00
|
|
|
Copyright (C) 2009 Lucas Murray <lmurray@undefinedfire.com>
|
2008-11-06 10:42:36 +00:00
|
|
|
|
|
|
|
This program 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.
|
|
|
|
|
|
|
|
This program 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 "monitor.h"
|
|
|
|
|
2013-11-01 11:29:05 +00:00
|
|
|
#include <QDebug>
|
2014-03-17 15:24:10 +00:00
|
|
|
#include <KLocalizedString>
|
2008-11-06 10:42:36 +00:00
|
|
|
#include <qgraphicsitem.h>
|
|
|
|
#include <qgraphicsview.h>
|
|
|
|
#include <qgraphicsscene.h>
|
|
|
|
#include <qgraphicssceneevent.h>
|
|
|
|
#include <qmenu.h>
|
2009-03-16 12:15:11 +00:00
|
|
|
#include <qdesktopwidget.h>
|
|
|
|
#include <qapplication.h>
|
|
|
|
#include <plasma/framesvg.h>
|
2008-11-06 10:42:36 +00:00
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
Monitor::Monitor(QWidget* parent)
|
|
|
|
: ScreenPreviewWidget(parent)
|
|
|
|
{
|
2009-03-16 12:15:11 +00:00
|
|
|
QDesktopWidget *desktop = QApplication::desktop();
|
|
|
|
QRect avail = desktop->availableGeometry(desktop->screenNumber(this));
|
2011-01-30 14:34:42 +00:00
|
|
|
setRatio((qreal)avail.width() / (qreal)avail.height());
|
|
|
|
for (int i = 0;
|
|
|
|
i < 8;
|
|
|
|
++i)
|
|
|
|
popups[ i ] = new QMenu(this);
|
|
|
|
scene = new QGraphicsScene(this);
|
|
|
|
view = new QGraphicsView(scene, this);
|
|
|
|
view->setBackgroundBrush(Qt::black);
|
|
|
|
view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
view->setFocusPolicy(Qt::NoFocus);
|
|
|
|
view->setFrameShape(QFrame::NoFrame);
|
|
|
|
for (int i = 0;
|
|
|
|
i < 8;
|
|
|
|
++i) {
|
|
|
|
items[ i ] = new Corner(this);
|
|
|
|
scene->addItem(items[ i ]);
|
2009-02-27 15:40:53 +00:00
|
|
|
hidden[ i ] = false;
|
2011-01-30 14:34:42 +00:00
|
|
|
grp[ i ] = new QActionGroup(this);
|
2008-11-06 10:42:36 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
checkSize();
|
|
|
|
}
|
2008-11-06 10:42:36 +00:00
|
|
|
|
|
|
|
void Monitor::clear()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
for (int i = 0;
|
|
|
|
i < 8;
|
|
|
|
++i) {
|
2008-11-06 10:42:36 +00:00
|
|
|
popups[ i ]->clear();
|
2011-01-30 14:34:42 +00:00
|
|
|
setEdge(i, false);
|
|
|
|
setEdgeHidden(i, false);
|
2008-11-06 10:42:36 +00:00
|
|
|
delete grp[ i ];
|
2011-01-30 14:34:42 +00:00
|
|
|
grp[ i ] = new QActionGroup(this);
|
2008-11-06 10:42:36 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2008-11-06 10:42:36 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void Monitor::resizeEvent(QResizeEvent* e)
|
|
|
|
{
|
|
|
|
ScreenPreviewWidget::resizeEvent(e);
|
2008-11-06 10:42:36 +00:00
|
|
|
checkSize();
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
|
|
|
|
2008-11-06 10:42:36 +00:00
|
|
|
void Monitor::checkSize()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2009-03-16 12:15:11 +00:00
|
|
|
QRect contentsRect = previewRect();
|
2009-10-05 09:47:13 +00:00
|
|
|
//int w = 151;
|
|
|
|
//int h = 115;
|
2009-03-16 12:15:11 +00:00
|
|
|
view->setGeometry(contentsRect);
|
2011-01-30 14:34:42 +00:00
|
|
|
scene->setSceneRect(QRect(QPoint(0, 0), contentsRect.size()));
|
|
|
|
int x2 = (contentsRect.width() - 20) / 2;
|
2009-03-16 12:15:11 +00:00
|
|
|
int x3 = contentsRect.width() - 20;
|
2011-01-30 14:34:42 +00:00
|
|
|
int y2 = (contentsRect.height() - 20) / 2;
|
2009-03-16 12:15:11 +00:00
|
|
|
int y3 = contentsRect.height() - 20;
|
2011-01-30 14:34:42 +00:00
|
|
|
items[ 0 ]->setRect(0, y2, 20, 20);
|
|
|
|
items[ 1 ]->setRect(x3, y2, 20, 20);
|
|
|
|
items[ 2 ]->setRect(x2, 0, 20, 20);
|
|
|
|
items[ 3 ]->setRect(x2, y3, 20, 20);
|
|
|
|
items[ 4 ]->setRect(0, 0, 20, 20);
|
|
|
|
items[ 5 ]->setRect(x3, 0, 20, 20);
|
|
|
|
items[ 6 ]->setRect(0, y3, 20, 20);
|
|
|
|
items[ 7 ]->setRect(x3, y3, 20, 20);
|
|
|
|
}
|
2008-11-06 10:42:36 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void Monitor::setEdge(int edge, bool set)
|
|
|
|
{
|
|
|
|
items[ edge ]->setActive(set);
|
|
|
|
}
|
2008-11-06 10:42:36 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
bool Monitor::edge(int edge) const
|
|
|
|
{
|
2008-11-06 10:42:36 +00:00
|
|
|
return items[ edge ]->brush() == Qt::green;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2008-11-06 10:42:36 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void Monitor::setEdgeHidden(int edge, bool set)
|
|
|
|
{
|
2009-02-27 15:40:53 +00:00
|
|
|
hidden[ edge ] = set;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (set)
|
2009-02-27 15:40:53 +00:00
|
|
|
items[ edge ]->hide();
|
|
|
|
else
|
|
|
|
items[ edge ]->show();
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2009-02-27 15:40:53 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
bool Monitor::edgeHidden(int edge) const
|
|
|
|
{
|
2009-02-27 15:40:53 +00:00
|
|
|
return hidden[ edge ];
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2009-02-27 15:40:53 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void Monitor::addEdgeItem(int edge, const QString& item)
|
|
|
|
{
|
|
|
|
QAction* act = popups[ edge ]->addAction(item);
|
|
|
|
act->setCheckable(true);
|
|
|
|
popup_actions[ edge ].append(act);
|
|
|
|
grp[ edge ]->addAction(act);
|
|
|
|
if (popup_actions[ edge ].count() == 1) {
|
|
|
|
act->setChecked(true);
|
|
|
|
items[ edge ]->setToolTip(item);
|
2008-11-06 10:42:36 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
setEdge(edge, !popup_actions[ edge ][ 0 ]->isChecked());
|
|
|
|
}
|
2008-11-06 10:42:36 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void Monitor::setEdgeItemEnabled(int edge, int index, bool enabled)
|
|
|
|
{
|
|
|
|
popup_actions[ edge ][ index ]->setEnabled(enabled);
|
|
|
|
}
|
2009-02-27 15:40:53 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
bool Monitor::edgeItemEnabled(int edge, int index) const
|
|
|
|
{
|
2009-02-27 15:40:53 +00:00
|
|
|
return popup_actions[ edge ][ index ]->isEnabled();
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2009-02-27 15:40:53 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void Monitor::selectEdgeItem(int edge, int index)
|
|
|
|
{
|
|
|
|
popup_actions[ edge ][ index ]->setChecked(true);
|
|
|
|
setEdge(edge, !popup_actions[ edge ][ 0 ]->isChecked());
|
2010-08-31 16:39:34 +00:00
|
|
|
QString actionText = popup_actions[ edge ][ index ]->text();
|
|
|
|
// remove accelerators added by KAcceleratorManager
|
2013-09-29 08:24:35 +00:00
|
|
|
actionText = KLocalizedString::removeAcceleratorMarker(actionText);
|
2011-01-30 14:34:42 +00:00
|
|
|
items[ edge ]->setToolTip(actionText);
|
|
|
|
}
|
2008-11-06 10:42:36 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
int Monitor::selectedEdgeItem(int edge) const
|
|
|
|
{
|
|
|
|
foreach (QAction * act, popup_actions[ edge ])
|
|
|
|
if (act->isChecked())
|
|
|
|
return popup_actions[ edge ].indexOf(act);
|
2008-11-06 10:42:36 +00:00
|
|
|
abort();
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2008-11-06 10:42:36 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void Monitor::popup(Corner* c, QPoint pos)
|
|
|
|
{
|
|
|
|
for (int i = 0;
|
|
|
|
i < 8;
|
|
|
|
++i) {
|
|
|
|
if (items[ i ] == c) {
|
|
|
|
if (popup_actions[ i ].count() == 0)
|
2008-11-06 10:42:36 +00:00
|
|
|
return;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (QAction* a = popups[ i ]->exec(pos)) {
|
|
|
|
selectEdgeItem(i, popup_actions[ i ].indexOf(a));
|
2008-11-06 10:42:36 +00:00
|
|
|
emit changed();
|
2011-01-30 14:34:42 +00:00
|
|
|
emit edgeSelectionChanged(i, popup_actions[ i ].indexOf(a));
|
2013-09-29 08:24:35 +00:00
|
|
|
c->setToolTip(KLocalizedString::removeAcceleratorMarker(a->text()));
|
2008-11-06 10:42:36 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
return;
|
2008-11-06 10:42:36 +00:00
|
|
|
}
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
abort();
|
|
|
|
}
|
2008-11-06 10:42:36 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void Monitor::flip(Corner* c, QPoint pos)
|
|
|
|
{
|
|
|
|
for (int i = 0;
|
|
|
|
i < 8;
|
|
|
|
++i) {
|
|
|
|
if (items[ i ] == c) {
|
|
|
|
if (popup_actions[ i ].count() == 0)
|
|
|
|
setEdge(i, !edge(i));
|
2008-11-06 10:42:36 +00:00
|
|
|
else
|
2011-01-30 14:34:42 +00:00
|
|
|
popup(c, pos);
|
2008-11-06 10:42:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
abort();
|
|
|
|
}
|
2008-11-06 10:42:36 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
Monitor::Corner::Corner(Monitor* m)
|
|
|
|
: monitor(m),
|
|
|
|
m_active(false),
|
|
|
|
m_hover(false)
|
|
|
|
{
|
2009-03-16 12:15:11 +00:00
|
|
|
button = new Plasma::FrameSvg();
|
|
|
|
button->setImagePath("widgets/button");
|
|
|
|
setAcceptHoverEvents(true);
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2009-03-16 12:15:11 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
Monitor::Corner::~Corner()
|
|
|
|
{
|
2009-03-16 12:15:11 +00:00
|
|
|
delete button;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2008-11-06 10:42:36 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void Monitor::Corner::contextMenuEvent(QGraphicsSceneContextMenuEvent* e)
|
|
|
|
{
|
|
|
|
monitor->popup(this, e->screenPos());
|
|
|
|
}
|
2008-11-06 10:42:36 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void Monitor::Corner::mousePressEvent(QGraphicsSceneMouseEvent* e)
|
|
|
|
{
|
|
|
|
monitor->flip(this, e->screenPos());
|
|
|
|
}
|
2008-11-06 10:42:36 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void Monitor::Corner::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
|
|
|
|
{
|
2009-03-16 12:15:11 +00:00
|
|
|
Q_UNUSED(option)
|
|
|
|
Q_UNUSED(widget)
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
if (m_hover) {
|
2009-03-16 12:15:11 +00:00
|
|
|
button->setElementPrefix("normal");
|
|
|
|
|
|
|
|
qreal left, top, right, bottom;
|
|
|
|
button->getMargins(left, top, right, bottom);
|
|
|
|
|
|
|
|
button->setElementPrefix("active");
|
|
|
|
qreal activeLeft, activeTop, activeRight, activeBottom;
|
|
|
|
button->getMargins(activeLeft, activeTop, activeRight, activeBottom);
|
|
|
|
|
|
|
|
QRectF activeRect = QRectF(QPointF(0, 0), rect().size());
|
|
|
|
activeRect.adjust(left - activeLeft, top - activeTop,
|
2011-01-30 14:34:42 +00:00
|
|
|
-(right - activeRight), -(bottom - activeBottom));
|
2009-03-16 12:15:11 +00:00
|
|
|
button->setElementPrefix("active");
|
|
|
|
button->resizeFrame(activeRect.size());
|
2011-01-30 14:34:42 +00:00
|
|
|
button->paintFrame(painter, rect().topLeft() + activeRect.topLeft());
|
|
|
|
} else {
|
|
|
|
button->setElementPrefix(m_active ? "pressed" : "normal");
|
2009-03-16 12:15:11 +00:00
|
|
|
button->resizeFrame(rect().size());
|
|
|
|
button->paintFrame(painter, rect().topLeft());
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2009-03-16 12:15:11 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
if (m_active) {
|
2009-03-16 12:15:11 +00:00
|
|
|
QPainterPath roundedRect;
|
|
|
|
painter->setRenderHint(QPainter::Antialiasing);
|
2011-01-30 14:34:42 +00:00
|
|
|
roundedRect.addRoundedRect(rect().adjusted(5, 5, -5, -5), 2, 2);
|
2009-03-16 12:15:11 +00:00
|
|
|
painter->fillPath(roundedRect, QApplication::palette().text());
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2009-03-16 12:15:11 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void Monitor::Corner::hoverEnterEvent(QGraphicsSceneHoverEvent * e)
|
2009-03-16 12:15:11 +00:00
|
|
|
{
|
2011-01-30 14:34:42 +00:00
|
|
|
Q_UNUSED(e);
|
2009-03-16 12:15:11 +00:00
|
|
|
m_hover = true;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void Monitor::Corner::hoverLeaveEvent(QGraphicsSceneHoverEvent * e)
|
2009-03-16 12:15:11 +00:00
|
|
|
{
|
2011-01-30 14:34:42 +00:00
|
|
|
Q_UNUSED(e);
|
2009-03-16 12:15:11 +00:00
|
|
|
m_hover = false;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void Monitor::Corner::setActive(bool active)
|
|
|
|
{
|
2009-03-16 12:15:11 +00:00
|
|
|
m_active = active;
|
|
|
|
update();
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2009-03-16 12:15:11 +00:00
|
|
|
|
|
|
|
bool Monitor::Corner::active() const
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2009-03-16 12:15:11 +00:00
|
|
|
return m_active;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2008-11-06 10:42:36 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
#include "monitor.moc"
|