add more spacing
[personal-kdebase.git] / workspace / kwin / kcmkwin / kwincompositing / monitor.cpp
blobe461c65dc6ad6ca32a79d2230eb65b0e7bc09208
1 /********************************************************************
2 KWin - the KDE window manager
3 This file is part of the KDE project.
5 Copyright (C) 2008 Lubos Lunak <l.lunak@suse.cz>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *********************************************************************/
21 #include "monitor.h"
23 #include <kdebug.h>
24 #include <kstandarddirs.h>
25 #include <qgraphicsitem.h>
26 #include <qgraphicsview.h>
27 #include <qgraphicsscene.h>
28 #include <qgraphicssceneevent.h>
29 #include <qmenu.h>
31 namespace KWin
34 Monitor::Monitor( QWidget* parent )
35 : QLabel( parent )
37 setPixmap( QPixmap( KStandardDirs::locate( "data", "kcontrol/pics/monitor.png" )));
38 for( int i = 0;
39 i < 8;
40 ++i )
41 popups[ i ] = new QMenu( this );
42 setAlignment( Qt::AlignCenter );
43 scene = new QGraphicsScene( this );
44 view = new QGraphicsView( scene, this );
45 view->setBackgroundBrush( Qt::black );
46 view->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
47 view->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
48 view->setFocusPolicy( Qt::NoFocus );
49 view->setFrameShape( QFrame::NoFrame );
50 for( int i = 0;
51 i < 8;
52 ++i )
54 items[ i ] = new Corner( this );
55 scene->addItem( items[ i ] );
56 items[ i ]->setBrush( Qt::red ); // TODO color-scheme dependent?
57 grp[ i ] = new QActionGroup( this );
59 checkSize();
62 void Monitor::clear()
64 for( int i = 0;
65 i < 8;
66 ++i )
68 popups[ i ]->clear();
69 setEdge( i, false );
70 delete grp[ i ];
71 grp[ i ] = new QActionGroup( this );
75 void Monitor::resizeEvent( QResizeEvent* e )
77 QLabel::resizeEvent( e );
78 checkSize();
81 void Monitor::checkSize()
83 int w = 151;
84 int h = 115;
85 view->setGeometry((width()-200)/2+23, (height()-186)/2+14, w, h );
86 scene->setSceneRect( 0, 0, w, h );
87 int x2 = ( w - 20 ) / 2;
88 int x3 = w - 20;
89 int y2 = ( h - 20 ) / 2;
90 int y3 = h - 20;
91 items[ 0 ]->setRect( 0, y2, 20, 20 );
92 items[ 1 ]->setRect( x3, y2, 20, 20 );
93 items[ 2 ]->setRect( x2, 0, 20, 20 );
94 items[ 3 ]->setRect( x2, y3, 20, 20 );
95 items[ 4 ]->setRect( 0, 0, 20, 20 );
96 items[ 5 ]->setRect( x3, 0, 20, 20 );
97 items[ 6 ]->setRect( 0, y3, 20, 20 );
98 items[ 7 ]->setRect( x3, y3, 20, 20 );
101 void Monitor::setEdge( int edge, bool set )
103 items[ edge ]->setBrush( set ? Qt::green : Qt::red );
106 bool Monitor::edge( int edge ) const
108 return items[ edge ]->brush() == Qt::green;
111 void Monitor::addEdgeItem( int edge, const QString& item )
113 QAction* act = popups[ edge ]->addAction( item );
114 act->setCheckable( true );
115 popup_actions[ edge ].append( act );
116 grp[ edge ]->addAction( act );
117 if( popup_actions[ edge ].count() == 1 )
118 act->setChecked( true );
119 setEdge( edge, !popup_actions[ edge ][ 0 ]->isChecked());
122 void Monitor::selectEdgeItem( int edge, int index )
124 popup_actions[ edge ][ index ]->setChecked( true );
125 setEdge( edge, !popup_actions[ edge ][ 0 ]->isChecked());
128 int Monitor::selectedEdgeItem( int edge ) const
130 foreach( QAction* act, popup_actions[ edge ] )
131 if( act->isChecked())
132 return popup_actions[ edge ].indexOf( act );
133 abort();
136 void Monitor::popup( Corner* c, QPoint pos )
138 for( int i = 0;
139 i < 8;
140 ++i )
142 if( items[ i ] == c )
144 if( popup_actions[ i ].count() == 0 )
145 return;
146 if( QAction* a = popups[ i ]->exec( pos ))
148 selectEdgeItem( i, popup_actions[ i ].indexOf( a ));
149 emit changed();
150 emit edgeSelectionChanged( i, popup_actions[ i ].indexOf( a ));
152 return;
155 abort();
158 void Monitor::flip( Corner* c, QPoint pos )
160 for( int i = 0;
161 i < 8;
162 ++i )
164 if( items[ i ] == c )
166 if( popup_actions[ i ].count() == 0 )
167 setEdge( i, !edge( i ));
168 else
169 popup( c, pos );
170 return;
173 abort();
176 Monitor::Corner::Corner( Monitor* m )
177 : monitor( m )
181 void Monitor::Corner::contextMenuEvent( QGraphicsSceneContextMenuEvent* e )
183 monitor->popup( this, e->screenPos());
186 void Monitor::Corner::mousePressEvent( QGraphicsSceneMouseEvent* e )
188 monitor->flip( this, e->screenPos());
191 } // namespace
193 #include "monitor.moc"