~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kwin/clients/tabstrip/tabstripbutton.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/********************************************************************
 
2
Tabstrip KWin window decoration
 
3
This file is part of the KDE project.
 
4
 
 
5
Copyright (C) 2009 Jorge Mata <matamax123@gmail.com>
 
6
Copyright (C) 2009 Lucas Murray <lmurray@undefinedfire.com>
 
7
 
 
8
This program is free software: you can redistribute it and/or modify
 
9
it under the terms of the GNU General Public License as published by
 
10
the Free Software Foundation, either version 2 of the License, or
 
11
(at your option) any later version.
 
12
 
 
13
This program is distributed in the hope that it will be useful,
 
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
GNU General Public License for more details.
 
17
 
 
18
You should have received a copy of the GNU General Public License
 
19
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
*********************************************************************/
 
21
 
 
22
#include <kcommondecoration.h>
 
23
#include "tabstripbutton.h"
 
24
#include "tabstripdecoration.h"
 
25
#include "tabstripfactory.h"
 
26
 
 
27
#include <KLocale>
 
28
 
 
29
#include <QPainter>
 
30
#include <QPalette>
 
31
#include <QPixmap>
 
32
#include <QRect>
 
33
 
 
34
TabstripButton::TabstripButton( ButtonType type, TabstripDecoration *parent, QString tip )
 
35
    : KCommonDecorationButton( type, parent ), SIZE( 16 )
 
36
    {
 
37
    setAutoFillBackground( false );
 
38
    setFixedSize( SIZE, SIZE );
 
39
    setCursor( Qt::ArrowCursor );
 
40
    client = parent;
 
41
    btype = type;
 
42
    setToolTip( tip );
 
43
    active_item = true;
 
44
    hovering = false;
 
45
    }
 
46
 
 
47
TabstripButton::~TabstripButton()
 
48
    {
 
49
    }
 
50
 
 
51
void TabstripButton::reset( unsigned long )
 
52
    {
 
53
    update();
 
54
    }
 
55
 
 
56
QSize TabstripButton::sizeHint() const
 
57
    {
 
58
    return QSize( SIZE, SIZE );
 
59
    }
 
60
 
 
61
void TabstripButton::paintEvent( QPaintEvent * )
 
62
    {
 
63
    QPainter p( this );
 
64
    const bool active = client->isActive() && active_item;
 
65
 
 
66
    // Icon geometry
 
67
    QRect geom = QRect( 3, 3, width() - 6, height() - 6 );
 
68
 
 
69
    // Determine painting colors
 
70
    QColor bgColor = client->options()->color( KDecoration::ColorTitleBar, active );
 
71
    QColor textColor = client->options()->color( KDecoration::ColorFont, active );
 
72
    if( hovering )
 
73
        { // Invert if the mouse cursor is hovering over the button
 
74
        textColor.setRed( 255 - textColor.red() );
 
75
        textColor.setGreen( 255 - textColor.green() );
 
76
        textColor.setBlue( 255 - textColor.blue() );
 
77
        }
 
78
 
 
79
    // Slight optimization as we are drawing solid straight lines
 
80
    p.setRenderHint( QPainter::Antialiasing, false );
 
81
 
 
82
    // Background
 
83
    p.fillRect( 0, 0, width(), height(), bgColor );
 
84
    //p.fillRect( 0, 0, width(), height(), QColor( 255, 0, 0 ));
 
85
 
 
86
    // Paint buttons with the text color
 
87
    p.setPen( textColor );
 
88
 
 
89
    switch( btype )
 
90
        {
 
91
        case HelpButton:
 
92
            {
 
93
            QFont font;
 
94
            font.setBold( true );
 
95
            p.setFont( font );
 
96
            p.drawText( geom.adjusted( 0, 1, 0, 0), Qt::AlignVCenter | Qt::AlignHCenter, "?" );
 
97
            }
 
98
            break;
 
99
        case MaxButton:
 
100
            switch( client->maximizeMode() )
 
101
                {
 
102
                case TabstripDecoration::MaximizeRestore:
 
103
                case TabstripDecoration::MaximizeVertical:
 
104
                case TabstripDecoration::MaximizeHorizontal:
 
105
                    // TL
 
106
                    p.drawLine( geom.x() + 3, geom.y(),
 
107
                        geom.x(), geom.y() );
 
108
                    p.drawLine( geom.x(), geom.y() + 3,
 
109
                        geom.x(), geom.y() );
 
110
                    p.drawLine( geom.x() + 3, geom.y() + 1,
 
111
                        geom.x(), geom.y() + 1 );
 
112
                    p.drawLine( geom.x() + 1, geom.y() + 3,
 
113
                        geom.x(), geom.y() + 1 );
 
114
                    // TR
 
115
                    p.drawLine( geom.x() + geom.width() - 3, geom.y(),
 
116
                        geom.x() + geom.width(), geom.y() );
 
117
                    p.drawLine( geom.x() + geom.width(), geom.y() + 3,
 
118
                        geom.x() + geom.width(), geom.y() );
 
119
                    p.drawLine( geom.x() + geom.width() - 3, geom.y() + 1,
 
120
                        geom.x() + geom.width(), geom.y() + 1 );
 
121
                    p.drawLine( geom.x() + geom.width() - 1, geom.y() + 3,
 
122
                        geom.x() + geom.width() - 1, geom.y() );
 
123
                    // BL
 
124
                    p.drawLine( geom.x() + 3, geom.y() + geom.height(),
 
125
                        geom.x(), geom.y() + geom.height() );
 
126
                    p.drawLine( geom.x(), geom.y() + geom.height() - 3,
 
127
                        geom.x(), geom.y() + geom.height() );
 
128
                    p.drawLine( geom.x() + 3, geom.y() + geom.height() - 1,
 
129
                        geom.x(), geom.y() + geom.height() - 1 );
 
130
                    p.drawLine( geom.x() + 1, geom.y() + geom.height() - 3,
 
131
                        geom.x() + 1, geom.y() + geom.height() );
 
132
                    // BR
 
133
                    p.drawLine( geom.x() + geom.width() - 3, geom.y() + geom.height(),
 
134
                        geom.x() + geom.width(), geom.y() + geom.height() );
 
135
                    p.drawLine( geom.x() + geom.width(), geom.y() + geom.height() - 3,
 
136
                        geom.x() + geom.width(), geom.y() + geom.height() );
 
137
                    p.drawLine( geom.x() + geom.width() - 3, geom.y() + geom.height() - 1,
 
138
                        geom.x() + geom.width(), geom.y() + geom.height() - 1 );
 
139
                    p.drawLine( geom.x() + geom.width() - 1, geom.y() + geom.height() - 3,
 
140
                        geom.x() + geom.width() - 1, geom.y() + geom.height() );
 
141
                    break;
 
142
                case TabstripDecoration::MaximizeFull:
 
143
                    // TL
 
144
                    p.drawLine( geom.x() + 2, geom.y(),
 
145
                        geom.x() + 2, geom.y() + 2 );
 
146
                    p.drawLine( geom.x(), geom.y() + 2,
 
147
                        geom.x() + 2, geom.y() + 2 );
 
148
                    p.drawLine( geom.x() + 3, geom.y(),
 
149
                        geom.x() + 3, geom.y() + 3 );
 
150
                    p.drawLine( geom.x(), geom.y() + 3,
 
151
                        geom.x() + 3, geom.y() + 3 );
 
152
                    // TR
 
153
                    p.drawLine( geom.x() + geom.width() - 2, geom.y(),
 
154
                        geom.x() + geom.width() - 2, geom.y() + 2 );
 
155
                    p.drawLine( geom.x() + geom.width(), geom.y() + 2,
 
156
                        geom.x() + geom.width() - 2, geom.y() + 2 );
 
157
                    p.drawLine( geom.x() + geom.width() - 3, geom.y(),
 
158
                        geom.x() + geom.width() - 3, geom.y() + 3 );
 
159
                    p.drawLine( geom.x() + geom.width(), geom.y() + 3,
 
160
                        geom.x() + geom.width() - 3, geom.y() + 3 );
 
161
                    // BL
 
162
                    p.drawLine( geom.x() + 2, geom.y() + geom.height(),
 
163
                        geom.x() + 2, geom.y() + geom.height() - 2 );
 
164
                    p.drawLine( geom.x(), geom.y() + geom.height() - 2,
 
165
                        geom.x() + 2, geom.y() + geom.height() - 2 );
 
166
                    p.drawLine( geom.x() + 3, geom.y() + geom.height(),
 
167
                        geom.x() + 3, geom.y() + geom.height() - 3 );
 
168
                    p.drawLine( geom.x(), geom.y() + geom.height() - 3,
 
169
                        geom.x() + 3, geom.y() + geom.height() - 3 );
 
170
                    // BR
 
171
                    p.drawLine( geom.x() + geom.width() - 2, geom.y() + geom.height(),
 
172
                        geom.x() + geom.width() - 2, geom.y() + geom.height() - 2 );
 
173
                    p.drawLine( geom.x() + geom.width(), geom.y() + geom.height() - 2,
 
174
                        geom.x() + geom.width() - 2, geom.y() + geom.height() - 2 );
 
175
                    p.drawLine( geom.x() + geom.width() - 3, geom.y() + geom.height(),
 
176
                        geom.x() + geom.width() - 3, geom.y() + geom.height() - 3 );
 
177
                    p.drawLine( geom.x() + geom.width(), geom.y() + geom.height() - 3,
 
178
                        geom.x() + geom.width() - 3, geom.y() + geom.height() - 3 );
 
179
                    break;
 
180
                }
 
181
            break;
 
182
        case MinButton:
 
183
            // B
 
184
            p.drawLine( geom.x(), geom.y() + geom.height(),
 
185
                geom.x() + geom.width(), geom.y() + geom.height() );
 
186
            p.drawLine( geom.x(), geom.y() + geom.height() - 1,
 
187
                geom.x() + geom.width(), geom.y() + geom.height() - 1 );
 
188
            // L
 
189
            p.drawLine( geom.x(), geom.y() + geom.height() - 3,
 
190
                geom.x(), geom.y() + geom.height() );
 
191
            p.drawLine( geom.x() + 1, geom.y() + geom.height() - 3,
 
192
                geom.x() + 1, geom.y() + geom.height() );
 
193
            // R
 
194
            p.drawLine( geom.x() + geom.width(), geom.y() + geom.height() - 3,
 
195
                geom.x() + geom.width(), geom.y() + geom.height() );
 
196
            p.drawLine( geom.x() + geom.width() - 1, geom.y() + geom.height() - 3,
 
197
                geom.x() + geom.width() - 1, geom.y() + geom.height() );
 
198
            break;
 
199
        case CloseButton:
 
200
        case ItemCloseButton:
 
201
            // TL-BR
 
202
            p.drawLine( geom.x() + 1, geom.y() + 1,
 
203
                geom.x() + geom.width() - 1, geom.y() + geom.height() - 1 );
 
204
            p.drawLine( geom.x() + 2, geom.y() + 1,
 
205
                geom.x() + geom.width() - 1, geom.y() + geom.height() - 2 );
 
206
            p.drawLine( geom.x() + 1, geom.y() + 2,
 
207
                geom.x() + geom.width() - 2, geom.y() + geom.height() - 1 );
 
208
            // TR-BL
 
209
            p.drawLine( geom.x() + 1, geom.y() + geom.height() - 1,
 
210
                geom.x() + geom.width() - 1, geom.y() + 1 );
 
211
            p.drawLine( geom.x() + 2, geom.y() + geom.height() - 1,
 
212
                geom.x() + geom.width() - 1, geom.y() + 2 );
 
213
            p.drawLine( geom.x() + 1, geom.y() + geom.height() - 2,
 
214
                geom.x() + geom.width() - 2, geom.y() + 1 );
 
215
            break;
 
216
        case MenuButton:
 
217
            if( client->clientGroupItems().count() > 1 || !TabstripFactory::showIcon() )
 
218
                {
 
219
                p.drawRect( geom.x(), geom.y() + geom.height() / 2 - 5,
 
220
                    1, 1 );
 
221
                p.drawRect( geom.x(), geom.y() + geom.height() / 2 - 1,
 
222
                    1, 1 );
 
223
                p.drawRect( geom.x(), geom.y() + geom.height() / 2 + 3,
 
224
                    1, 1 );
 
225
                p.drawRect( geom.x() + 4, geom.y() + geom.height() / 2 - 5,
 
226
                    geom.width() - 5, 1 );
 
227
                p.drawRect( geom.x() + 4, geom.y() + geom.height() / 2 - 1,
 
228
                    geom.width() - 5, 1 );
 
229
                p.drawRect( geom.x() + 4, geom.y() + geom.height() / 2 + 3,
 
230
                    geom.width() - 5, 1 );
 
231
                }
 
232
            else
 
233
                p.drawPixmap( 0, 0, client->icon().pixmap( SIZE ));
 
234
            break;
 
235
        case OnAllDesktopsButton:
 
236
            {
 
237
            if( isChecked() )
 
238
                p.fillRect( geom.x() + geom.width() / 2 - 1, geom.y() + geom.height() / 2 - 1,
 
239
                    3, 3, textColor );
 
240
            else
 
241
                {
 
242
                p.fillRect( geom.x() + geom.width() / 2 - 4, geom.y() + geom.height() / 2 - 4,
 
243
                    3, 3, textColor );
 
244
                p.fillRect( geom.x() + geom.width() / 2 - 4, geom.y() + geom.height() / 2 + 2,
 
245
                    3, 3, textColor );
 
246
                p.fillRect( geom.x() + geom.width() / 2 + 2, geom.y() + geom.height() / 2 - 4,
 
247
                    3, 3, textColor );
 
248
                p.fillRect( geom.x() + geom.width() / 2 + 2, geom.y() + geom.height() / 2 + 2,
 
249
                    3, 3, textColor );
 
250
                }
 
251
            }
 
252
            break;
 
253
        case AboveButton:
 
254
            {
 
255
            int o = -2;
 
256
            if( isChecked() )
 
257
                {
 
258
                o = -4;
 
259
                p.drawRect( geom.x() + geom.width() / 2 - 4, geom.y() + geom.height() / 2 + 3,
 
260
                    8, 1 );
 
261
                }
 
262
            p.drawPoint( geom.x() + geom.width() / 2, geom.y() + geom.height() / 2 + o );
 
263
            p.drawLine( geom.x() + geom.width() / 2 - 1, geom.y() + geom.height() / 2 + o + 1,
 
264
                geom.x() + geom.width() / 2 + 1, geom.y() + geom.height() / 2 + o + 1 );
 
265
            p.drawLine( geom.x() + geom.width() / 2 - 2, geom.y() + geom.height() / 2 + o + 2,
 
266
                geom.x() + geom.width() / 2 + 2, geom.y() + geom.height() / 2 + o + 2 );
 
267
            p.drawLine( geom.x() + geom.width() / 2 - 3, geom.y() + geom.height() / 2 + o + 3,
 
268
                geom.x() + geom.width() / 2 + 3, geom.y() + geom.height() / 2 + o + 3 );
 
269
            p.drawLine( geom.x() + geom.width() / 2 - 4, geom.y() + geom.height() / 2 + o + 4,
 
270
                geom.x() + geom.width() / 2 + 4, geom.y() + geom.height() / 2 + o + 4 );
 
271
            }
 
272
            break;
 
273
        case BelowButton:
 
274
            {
 
275
            int o = 1;
 
276
            if( isChecked() )
 
277
                {
 
278
                o = 3;
 
279
                p.drawRect( geom.x() + geom.width() / 2 - 4, geom.y() + geom.height() / 2 - 5,
 
280
                    8, 1 );
 
281
                }
 
282
            p.drawPoint( geom.x() + geom.width() / 2, geom.y() + geom.height() / 2 + o );
 
283
            p.drawLine( geom.x() + geom.width() / 2 - 1, geom.y() + geom.height() / 2 + o - 1,
 
284
                geom.x() + geom.width() / 2 + 1, geom.y() + geom.height() / 2 + o - 1 );
 
285
            p.drawLine( geom.x() + geom.width() / 2 - 2, geom.y() + geom.height() / 2 + o - 2,
 
286
                geom.x() + geom.width() / 2 + 2, geom.y() + geom.height() / 2 + o - 2 );
 
287
            p.drawLine( geom.x() + geom.width() / 2 - 3, geom.y() + geom.height() / 2 + o - 3,
 
288
                geom.x() + geom.width() / 2 + 3, geom.y() + geom.height() / 2 + o - 3 );
 
289
            p.drawLine( geom.x() + geom.width() / 2 - 4, geom.y() + geom.height() / 2 + o - 4,
 
290
                geom.x() + geom.width() / 2 + 4, geom.y() + geom.height() / 2 + o - 4 );
 
291
            }
 
292
            break;
 
293
        case ShadeButton:
 
294
            p.drawLine( geom.x(), geom.y(),
 
295
                geom.x() + geom.width(), geom.y() );
 
296
            p.drawLine( geom.x(), geom.y() + 1,
 
297
                geom.x() + geom.width(), geom.y() + 1 );
 
298
            break;
 
299
        case NumButtons:
 
300
        default:
 
301
            break;
 
302
        };
 
303
    }
 
304
 
 
305
void TabstripButton::enterEvent( QEvent *e )
 
306
    {
 
307
    hovering = true;
 
308
    repaint();
 
309
    KCommonDecorationButton::enterEvent( e );
 
310
    }
 
311
 
 
312
void TabstripButton::leaveEvent( QEvent *e )
 
313
    {
 
314
    hovering = false;
 
315
    repaint();
 
316
    KCommonDecorationButton::leaveEvent( e );
 
317
    }