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 *********************************************************************/
24 #include <kstandarddirs.h>
25 #include <qgraphicsitem.h>
26 #include <qgraphicsview.h>
27 #include <qgraphicsscene.h>
28 #include <qgraphicssceneevent.h>
34 Monitor::Monitor( QWidget
* parent
)
37 setPixmap( QPixmap( KStandardDirs::locate( "data", "kcontrol/pics/monitor.png" )));
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
);
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 );
71 grp
[ i
] = new QActionGroup( this );
75 void Monitor::resizeEvent( QResizeEvent
* e
)
77 QLabel::resizeEvent( e
);
81 void Monitor::checkSize()
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;
89 int y2
= ( h
- 20 ) / 2;
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
);
136 void Monitor::popup( Corner
* c
, QPoint pos
)
142 if( items
[ i
] == c
)
144 if( popup_actions
[ i
].count() == 0 )
146 if( QAction
* a
= popups
[ i
]->exec( pos
))
148 selectEdgeItem( i
, popup_actions
[ i
].indexOf( a
));
150 emit
edgeSelectionChanged( i
, popup_actions
[ i
].indexOf( a
));
158 void Monitor::flip( Corner
* c
, QPoint pos
)
164 if( items
[ i
] == c
)
166 if( popup_actions
[ i
].count() == 0 )
167 setEdge( i
, !edge( i
));
176 Monitor::Corner::Corner( 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());
193 #include "monitor.moc"