summaryrefslogtreecommitdiffstats
path: root/ksirc/colorpicker.cpp
blob: 9bb5f03f06efe6223ac57c8e47e858bf860cd598 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* This file is part of the KDE project
   Copyright (C) 2001 Simon Hausmann <[email protected]>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "colorpicker.h"
#include "ksopts.h"

#include <tqlayout.h>
#include <tqpainter.h>
#include <tqvbox.h>
#include <tqstyle.h>
#include <tqlineedit.h>
#include <tqlabel.h>
#include <tqpushbutton.h>

#include <klocale.h>

ColorPicker::ColorPicker( TQWidget *parent, const char *name )
    : KDialogBase( parent, name, true /*modal*/, i18n( "Pick Color" ),
                   KDialogBase::Ok | KDialogBase::Cancel,
                   KDialogBase::Cancel ),
      m_foregroundColor( -1 ), m_backgroundColor( -1 )
{
    TQVBox *mainWidget = makeVBoxMainWidget();

    TQWidget *sampleBox = new TQWidget( mainWidget );
    TQHBoxLayout *sampleLayout = new TQHBoxLayout( sampleBox );

    TQLabel *preview = new TQLabel( i18n( "Preview:" ), sampleBox );
    sampleLayout->addWidget( preview );

    m_sample = new TQLineEdit( i18n( "Sample Text" ), sampleBox );
    m_sample->setFocusPolicy( NoFocus );
    m_sample->setSizePolicy( TQSizePolicy( TQSizePolicy::Minimum,
                             m_sample->sizePolicy().verData() ) );
    sampleLayout->addWidget( m_sample );
    sampleLayout->addStretch();

    TQHBox *box = new TQHBox( mainWidget );
    TQLabel *description = new TQLabel( i18n( "&Foreground:" ), box );
    ColorBar *foregroundColor = new ColorBar( ksopts->ircColors.toValueVector(), box );
    description->setBuddy( foregroundColor );

    box = new TQHBox( mainWidget );
    description = new TQLabel( i18n( "&Background:" ), box );
    ColorBar *backgroundColor = new ColorBar( ksopts->ircColors.toValueVector(), box );
    description->setBuddy( backgroundColor );

    TQPushButton *ok = actionButton( KDialogBase::Ok );
    TQPushButton *cancel = actionButton( KDialogBase::Cancel );

    setTabOrder( foregroundColor, backgroundColor );
    setTabOrder( backgroundColor, ok );
    setTabOrder( ok, cancel );

    ok->setAutoDefault( false );
    cancel->setAutoDefault( false );

    connect( foregroundColor, TQT_SIGNAL( colorPicked( int ) ),
             this, TQT_SLOT( setForegroundColor( int ) ) );
    connect( backgroundColor, TQT_SIGNAL( colorPicked( int ) ),
             this, TQT_SLOT( setBackgroundColor( int ) ) );

    ok->setEnabled( false );

    updateSample();
}

TQString ColorPicker::colorString() const
{
    assert( m_foregroundColor != -1 );
    TQString res( TQString::number( m_foregroundColor ) );
    if ( m_backgroundColor != -1 )
    {
        res += ',';
        res += TQString::number( m_backgroundColor );
    }
    return res;
}

void ColorPicker::setForegroundColor( int col )
{
    TQPushButton * ok =actionButton( KDialogBase::Ok );
    assert( ok );
    ok->setEnabled( true );

    m_foregroundColor = col;
    updateSample();
}

void ColorPicker::setBackgroundColor( int col )
{
    m_backgroundColor = col;
    updateSample();
}

void ColorPicker::updateSample()
{
    TQColorGroup cg( colorGroup() );

    TQColor col = ksopts->textColor;
    if ( m_foregroundColor != -1 )
        col = ksopts->ircColors[ m_foregroundColor ];

    cg.setColor( TQColorGroup::Foreground, col );
    cg.setColor( TQColorGroup::Text, col );

    if ( m_backgroundColor != -1 )
    {
        col = ksopts->ircColors[ m_backgroundColor ];
        cg.setColor( TQColorGroup::Background, col );
        cg.setColor( TQColorGroup::Base, col );
    }

    m_sample->setPalette( TQPalette( cg, cg, cg ) );
}

ColorBar::ColorBar( const TQValueVector<TQColor> &colors, TQWidget *parent, 
                      const char *name )
    : TQFrame( parent, name, WStaticContents | WRepaintNoErase ),
      m_currentCell( -1 ), m_focusedCell( - 1 ), m_colors( colors ), 
      m_cellSize( 0 )
{
    setFrameStyle( StyledPanel | Sunken );

    updateCellSize();

    setFocusPolicy( StrongFocus );
}

void ColorBar::drawContents( TQPainter *p )
{
    int x = contentsRect().x();
    int y = contentsRect().y();
    for ( unsigned int i = 0; i < m_colors.size(); ++i, x += m_cellSize )
    {
        bool isCurrentCell = ( m_currentCell != -1 && 
                               i == static_cast<uint>( m_currentCell ) );
        bool isFocusedCell = ( m_focusedCell != -1 && 
                               i == static_cast<uint>( m_focusedCell ) );
        drawCell( p, x, y, m_colors[ i ], TQString::number( i ), 
                  isFocusedCell, isCurrentCell );
    }
}

void ColorBar::keyPressEvent( TQKeyEvent *ev )
{
    if ( m_focusedCell == -1 ) {
        TQFrame::keyPressEvent( ev );
        return;
    }

    switch ( ev->key() )
    {
        case Key_Left:
            if ( m_focusedCell > 1 )
                m_focusedCell--;
            update();
            ev->accept();
            return;
        case Key_Right:
            if ( static_cast<uint>( m_focusedCell ) < m_colors.size() - 1 )
                m_focusedCell++;
            update();
            ev->accept();
            return;
        case Key_Enter:
        case Key_Return:
        case Key_Space:
            setCurrentCell( m_focusedCell );
            update();
            ev->accept();
            return;
        default: break;
    }

    TQFrame::keyPressEvent( ev );
}

void ColorBar::focusInEvent( TQFocusEvent *ev )
{
    if ( ev->reason() == TQFocusEvent::Tab ||
         ev->reason() == TQFocusEvent::Backtab )
        m_focusedCell = 0;
    TQFrame::focusInEvent( ev );
}

void ColorBar::focusOutEvent( TQFocusEvent *ev )
{
    if ( ev->reason() == TQFocusEvent::Tab ||
         ev->reason() == TQFocusEvent::Backtab ||
         ev->reason() == TQFocusEvent::Mouse )
        m_focusedCell = -1;
    TQFrame::focusOutEvent( ev );
}

void ColorBar::fontChange( const TQFont &oldFont )
{
    updateCellSize();
    TQFrame::fontChange( oldFont );
}

void ColorBar::styleChange( TQStyle &oldStyle )
{
    updateCellSize();
    TQFrame::styleChange( oldStyle );
}

bool ColorBar::focusNextPrevChild( bool next )
{
    if ( next )
    {
        assert( m_focusedCell != -1 );

        if ( static_cast<uint>( m_focusedCell ) < m_colors.size() - 1 )
        {
            m_focusedCell++;
            update();
            return true;
        }
        return TQFrame::focusNextPrevChild( next );
    }

    if ( m_focusedCell > 1 )
    {
        m_focusedCell--;
        update();
        return true;
    }

    return TQFrame::focusNextPrevChild( next );
}

void ColorBar::mousePressEvent( TQMouseEvent *ev )
{
    const TQPoint &p = ev->pos();
    if ( contentsRect().contains( p ) )
    {
        m_focusedCell = p.x() / m_cellSize;
        update();
    }

    TQFrame::mousePressEvent( ev );
}

void ColorBar::mouseReleaseEvent( TQMouseEvent *ev )
{
    if ( m_focusedCell != -1 )
    {
        setCurrentCell( m_focusedCell );
        update();
    }
    TQFrame::mouseReleaseEvent( ev );
}

void ColorBar::updateCellSize()
{
    setLineWidth( style().pixelMetric( TQStyle::PM_DefaultFrameWidth, this ) );

    TQFontMetrics metrics( font() );

    m_cellSize = metrics.width( TQString::number( m_colors.size() ) ) +
                 ( s_indicatorSize * 2 ) +
                 ( s_focusSize * 2 ) +
                 ( s_innerMargin * 2 );

    setFixedSize( TQSize( ( m_colors.size() * m_cellSize ) + ( frameWidth() * 2 ), 
                         m_cellSize + ( frameWidth() * 2 ) ) );
}

void ColorBar::setCurrentCell( int cell )
{
    m_currentCell = cell;
    emit colorPicked( cell );
}

void ColorBar::drawCell( TQPainter *p, int x, int y, const TQColor &color,
                         const TQString &text, bool isFocusedCell, 
                         bool isCurrentCell )
{
    p->fillRect( x, y, m_cellSize, m_cellSize, color );

    TQColor penColor = black;
    // ### hack
    if ( color.red() < 127 && color.green() < 127 && color.blue() < 127 )
        penColor = white;
        
    p->setPen( penColor );

    if ( isCurrentCell )
    {
        p->fillRect( x, y, m_cellSize, s_indicatorSize, penColor );
        p->fillRect( x, y + s_indicatorSize, 
                     s_indicatorSize, m_cellSize - ( 2 * s_indicatorSize ), penColor );
        p->fillRect( x, y + m_cellSize - s_indicatorSize, 
                     m_cellSize, s_indicatorSize, penColor );
        p->fillRect( x + m_cellSize - s_indicatorSize, y + s_indicatorSize,
                     s_indicatorSize, m_cellSize - ( 2 * s_indicatorSize ), penColor );
    }

    if ( isFocusedCell )
    {
        int focusRectSize = m_cellSize - ( 2 * s_indicatorSize );

        p->fillRect( x + s_indicatorSize, y + s_indicatorSize, 
                     focusRectSize, s_focusSize, penColor );
        p->fillRect( x + s_indicatorSize, y + s_indicatorSize + s_focusSize, 
                     s_focusSize, focusRectSize - ( 2 * s_focusSize ), penColor );
        p->fillRect( x + s_indicatorSize, 
                     y + m_cellSize - s_indicatorSize - s_focusSize,
                     focusRectSize, s_focusSize, penColor );
        p->fillRect( x + m_cellSize - s_indicatorSize - s_focusSize, 
                     y + s_indicatorSize + s_focusSize, 
                     s_focusSize, focusRectSize - ( 2 * s_focusSize ), penColor );
    }

    TQFontMetrics metrics( p->font() );

    int offset = ( m_cellSize / 2 ) - ( metrics.width( text ) / 2 );
    p->drawText( x + offset, y + s_focusSize + s_indicatorSize + 
                 + metrics.ascent(), text );
}
 
#include "colorpicker.moc"

/* vim: et sw=4
 */