summaryrefslogtreecommitdiffstats
path: root/tdeui/kcolorbutton.cpp
blob: 37ab8a111b878f2def5256b3fa429bd4d2488308 (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
/*  This file is part of the KDE libraries
    Copyright (C) 1997 Martin Jones (mjones@kde.org)
    Copyright (C) 1999 Cristian Tibirna (ctibirna@kde.org)

    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 <config.h>

#include <tqpainter.h>
#include <tqdrawutil.h>
#include <tqapplication.h>
#include <tqclipboard.h>
#include <tqstyle.h>
#include <tdeglobalsettings.h>
#include <tdestdaccel.h>
#include "kcolordialog.h"
#include "kcolorbutton.h"
#include "kcolordrag.h"

class KColorButton::KColorButtonPrivate
{
public:
    bool m_bdefaultColor;
    TQColor m_defaultColor;
};

KColorButton::KColorButton( TQWidget *parent, const char *name )
  : TQPushButton( parent, name )
{
  d = new KColorButtonPrivate;
  d->m_bdefaultColor = false;
  d->m_defaultColor = TQColor();
  setAcceptDrops( true);

  // 2000-10-15 (putzer): fixes broken keyboard usage
  connect (this, TQT_SIGNAL(clicked()), this, TQT_SLOT(chooseColor()));
}

KColorButton::KColorButton( const TQColor &c, TQWidget *parent,
			    const char *name )
  : TQPushButton( parent, name ), col(c)
{
  d = new KColorButtonPrivate;
  d->m_bdefaultColor = false;
  d->m_defaultColor = TQColor();
  setAcceptDrops( true);

  // 2000-10-15 (putzer): fixes broken keyboard usage
  connect (this, TQT_SIGNAL(clicked()), this, TQT_SLOT(chooseColor()));
}

KColorButton::KColorButton( const TQColor &c, const TQColor &defaultColor, TQWidget *parent,
			    const char *name )
  : TQPushButton( parent, name ), col(c)
{
  d = new KColorButtonPrivate;
  d->m_bdefaultColor = true;
  d->m_defaultColor = defaultColor;
  setAcceptDrops( true);

  // 2000-10-15 (putzer): fixes broken keyboard usage
  connect (this, TQT_SIGNAL(clicked()), this, TQT_SLOT(chooseColor()));
}

KColorButton::~KColorButton()
{
  delete d;
}

void KColorButton::setColor( const TQColor &c )
{
  if ( col != c ) {
    col = c;
    repaint( false );
    emit changed( col );
  }
}

TQColor KColorButton::defaultColor() const
{
  return d->m_defaultColor;
}

void KColorButton::setDefaultColor( const TQColor &c )
{
  d->m_bdefaultColor = c.isValid();
  d->m_defaultColor = c;
}


void KColorButton::drawButtonLabel( TQPainter *painter )
{
  int x, y, w, h;
  TQRect r = style().subRect( TQStyle::SR_PushButtonContents, this );
  r.rect(&x, &y, &w, &h);

  int margin = style().pixelMetric( TQStyle::PM_ButtonMargin, this );
  x += margin;
  y += margin;
  w -= 2*margin;
  h -= 2*margin;

  if (isOn() || isDown()) {
    x += style().pixelMetric( TQStyle::PM_ButtonShiftHorizontal, this );
    y += style().pixelMetric( TQStyle::PM_ButtonShiftVertical, this );
  }

  TQColor fillCol = isEnabled() ? col : backgroundColor();
  qDrawShadePanel( painter, x, y, w, h, colorGroup(), true, 1, NULL);
  if ( fillCol.isValid() )
    painter->fillRect( x+1, y+1, w-2, h-2, fillCol );

  if ( hasFocus() ) {
    TQRect focusRect = style().subRect( TQStyle::SR_PushButtonFocusRect, this );
    style().tqdrawPrimitive( TQStyle::PE_FocusRect, painter, focusRect, colorGroup() );
  }
}

TQSize KColorButton::sizeHint() const
{
  return style().tqsizeFromContents(TQStyle::CT_PushButton, this, TQSize(40, 15)).
	  	expandedTo(TQApplication::globalStrut());
}

void KColorButton::dragEnterEvent( TQDragEnterEvent *event)
{
  event->accept( KColorDrag::canDecode( event) && isEnabled());
}

void KColorButton::dropEvent( TQDropEvent *event)
{
  TQColor c;
  if( KColorDrag::decode( event, c)) {
    setColor(c);
  }
}

void KColorButton::keyPressEvent( TQKeyEvent *e )
{
  KKey key( e );

  if ( TDEStdAccel::copy().contains( key ) ) {
    TQMimeSource* mime = new KColorDrag( color() );
    TQApplication::clipboard()->setData( mime, TQClipboard::Clipboard );
  }
  else if ( TDEStdAccel::paste().contains( key ) ) {
    TQColor color;
    KColorDrag::decode( TQApplication::clipboard()->data( TQClipboard::Clipboard ), color );
    setColor( color );
  }
  else
    TQPushButton::keyPressEvent( e );
}

void KColorButton::mousePressEvent( TQMouseEvent *e)
{
  mPos = e->pos();
  TQPushButton::mousePressEvent(e);
}

void KColorButton::mouseMoveEvent( TQMouseEvent *e)
{
  if( (e->state() & TQt::LeftButton) &&
    (e->pos()-mPos).manhattanLength() > TDEGlobalSettings::dndEventDelay() )
  {
    // Drag color object
    KColorDrag *dg = new KColorDrag( color(), this);
    dg->dragCopy();
    setDown(false);
  }
}

void KColorButton::chooseColor()
{
  TQColor c = color();
  if ( d->m_bdefaultColor )
  {
      if( KColorDialog::getColor( c, d->m_defaultColor, this ) != TQDialog::Rejected ) {
          setColor( c );
      }
  }
  else
  {
      if( KColorDialog::getColor( c, this ) != TQDialog::Rejected ) {
          setColor( c );
      }
  }
}

void KColorButton::virtual_hook( int, void* )
{ /*BASE::virtual_hook( id, data );*/ }

#include "kcolorbutton.moc"