summaryrefslogtreecommitdiffstats
path: root/tdeiconedit/palettetoolbar.cpp
blob: 879acdb7e7c02c411f2e94db7f25277c94ac93fa (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
/*
    tdeiconedit - a small graphics drawing program for the KDE
    Copyright ( C ) 1998  Thomas Tanghus (  tanghus@kde.org )
    Copyright ( C ) 2002  Nadeem Hasan (  nhasan@kde.org )

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

    This program 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
    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 <tqlabel.h>
#include <tqlayout.h>
#include <tqwhatsthis.h>
#include <tqpainter.h>

#include <tdelocale.h>
#include <kdebug.h>

#include "tdeiconcolors.h"
#include "palettetoolbar.h"

PaletteToolBar::PaletteToolBar( TQWidget *parent, const char *name )
    : TDEToolBar( parent, name )
{
  TQWidget *base = new TQWidget( this );

  TQBoxLayout::Direction d = orientation() == TQt::Horizontal? 
      TQBoxLayout::LeftToRight : TQBoxLayout::TopToBottom;
  m_layout = new TQBoxLayout( base, d, 2, 6 );

  m_lblPreview = new TQLabel( base );
  m_lblPreview->setFrameStyle( TQFrame::Panel|TQFrame::Sunken );
  m_lblPreview->setFixedHeight( 64 );
  m_lblPreview->setAlignment( TQt::AlignHCenter|TQt::AlignVCenter );
  TQWhatsThis::add(m_lblPreview, i18n( "Preview\n\nThis is a 1:1 preview"
      " of the current icon" ) );
  m_layout->addWidget( m_lblPreview );

  m_currentColorView = new TQLabel( base );
  m_currentColorView->setFrameStyle( TQFrame::Panel|TQFrame::Sunken );
  m_currentColorView->setFixedHeight( 24 );
  m_currentColorView->setAlignment( TQt::AlignHCenter|TQt::AlignVCenter );
  TQWhatsThis::add(m_currentColorView, i18n( "Current color\n\nThis is the currently selected color" ) );
  m_layout->addWidget( m_currentColorView );

  TQVBoxLayout *vlayout = new TQVBoxLayout( m_layout, 0 );
  TQLabel *l = new TQLabel( i18n( "System colors:" ), base );
  vlayout->addWidget( l );
  m_sysColors = new KSysColors( base );
  TQWhatsThis::add(m_sysColors, i18n( "System colors\n\nHere you can select"
      " colors from the TDE icon palette" ) );

  vlayout->addWidget( m_sysColors );

  connect( m_sysColors, TQ_SIGNAL( newColor(uint) ), 
      TQ_SIGNAL( newColor(uint) ) );

  vlayout = new TQVBoxLayout( m_layout, 0 );
  l = new TQLabel( i18n( "Custom colors:" ), base );
  vlayout->addWidget( l );
  m_customColors = new KCustomColors( base );
  TQWhatsThis::add(m_customColors, i18n( "Custom colors\n\nHere you can"
      " build a palette of custom colors.\nDouble-click on a box to edit"
      " the color" ) );

  vlayout->addWidget( m_customColors );

  connect( m_customColors, TQ_SIGNAL( newColor(uint) ), 
      TQ_SIGNAL( newColor(uint) ) );
  connect( this, TQ_SIGNAL( newColor(uint)),
      this, TQ_SLOT(currentColorChanged(uint)));
  currentColorChanged(OPAQUE_MASK|0);

  setEnableContextMenu( false );
  setMovingEnabled( false );
}

void PaletteToolBar::setOrientation( TQt::Orientation o )
{
   if(  barPos() == Floating )
     o = o == TQt::Vertical ? TQt::Horizontal : TQt::Vertical;

  TQBoxLayout::Direction d = o == TQt::Horizontal? TQBoxLayout::LeftToRight
        : TQBoxLayout::TopToBottom;
  m_layout->setDirection( d );

  TQDockWindow::setOrientation( o );
}

void PaletteToolBar::previewChanged(  const TQPixmap &p )
{
  m_lblPreview->setPixmap( p );
}

void PaletteToolBar::addColors( uint n, uint *c )
{
    m_customColors->clear();
    for( uint i = 0; i < n; i++ )
        addColor( c[ i ] );
}

void PaletteToolBar::addColor( uint color )
{
    if( !m_sysColors->contains( color ) )
        m_customColors->addColor( color );
}

void PaletteToolBar::setPreviewBackground( TQPixmap pixmap )
{
    m_lblPreview->setBackgroundPixmap(pixmap);
}

void PaletteToolBar::setPreviewBackground( const TQColor& colour )
{
    m_lblPreview->setBackgroundColor(colour);
}

void PaletteToolBar::currentColorChanged(uint color)
{
  if(tqAlpha(color) == 255)
  {
    m_currentColorView->setBackgroundColor(color);
  }
  else
  {
    // Show the colour as if drawn over a checkerboard pattern
    const int squareWidth = 8;
    const uint lightColour = tqRgb(255, 255, 255);
    const uint darkColour = tqRgb(127, 127, 127);

    TQPixmap pm(2 * squareWidth, 2 * squareWidth);
    TQPainter p(&pm);

    double alpha = tqAlpha(color) / 255.0;

    int r = int(tqRed(color) * alpha + (1 - alpha) * tqRed(lightColour) + 0.5);
    int g = int(tqGreen(color) * alpha + (1 - alpha) * tqGreen(lightColour) + 0.5);
    int b = int(tqBlue(color) * alpha + (1 - alpha) * tqBlue(lightColour) + 0.5);

    uint squareColour = tqRgb(r, g, b);

    p.setPen(TQt::NoPen);
    p.setBrush(TQColor(squareColour));
    p.drawRect(0, 0, squareWidth, squareWidth);
    p.drawRect(squareWidth, squareWidth, squareWidth, squareWidth);

    r = int(tqRed(color) * alpha + (1 - alpha) * tqRed(darkColour) + 0.5);
    g = int(tqGreen(color) * alpha + (1 - alpha) * tqGreen(darkColour) + 0.5);
    b = int(tqBlue(color) * alpha + (1 - alpha) * tqBlue(darkColour) + 0.5);

    squareColour = tqRgb(r, g, b);

    p.setBrush(TQColor(squareColour));
    p.drawRect(squareWidth, 0, squareWidth, squareWidth);
    p.drawRect(0, squareWidth, squareWidth, squareWidth);

    p.end();

    m_currentColorView->setBackgroundPixmap(pm);
  }
}

#include "palettetoolbar.moc"