F:/KPlato/koffice/libs/kofficeui/KoTooluButton.cpp

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2002 Werner Trobin <trobin@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include <QApplication>
00021 #include <QToolTip>
00022 #include <QPainter>
00023 #include <qdrawutil.h>
00024 #include <QPixmap>
00025 #include <QStyle>
00026 #include <q3popupmenu.h>
00027 //Added by qt3to4:
00028 #include <QFocusEvent>
00029 #include <QPaintEvent>
00030 #include <QKeyEvent>
00031 #include <QEvent>
00032 #include <QDropEvent>
00033 #include <QDragEnterEvent>
00034 #include <QMouseEvent>
00035 
00036 #include <kglobalsettings.h>
00037 #include <ktoolbar.h>
00038 #include <KoTooluButton.h>
00039 #include <k3colordrag.h>
00040 #include <klocale.h>
00041 #include <kcolordialog.h>
00042 #include <kdebug.h>
00043 
00044 namespace {
00045     // For the KoColorPanel
00046     const int COLS = 15;
00047     const int TILESIZE = 16;
00048     // For the KoToolButton
00049     int ARROW_WIDTH = 12;
00050 }
00051 
00052 KoColorPanel::KoColorPanel( QWidget* parent, const char* name ) :
00053     QWidget( parent, name, Qt::WStaticContents | Qt::WNoAutoErase | Qt::WResizeNoErase )
00054 {
00055     setMouseTracking( true );
00056     setAcceptDrops( true );
00057     init();
00058 }
00059 
00060 KoColorPanel::~KoColorPanel()
00061 {
00062 }
00063 
00064 QSize KoColorPanel::sizeHint() const
00065 {
00066     return minimumSizeHint();
00067 }
00068 
00069 QSize KoColorPanel::minimumSizeHint() const
00070 {
00071     return QSize( COLS << 4, lines() << 4 );
00072 }
00073 
00074 Q3PopupMenu* KoColorPanel::createColorPopup( KoColorPanel::MenuStyle style, const QColor& defaultColor,
00075                                             const QObject* receiver, const char* slot,
00076                                             QWidget* parent, const char* name )
00077 {
00078     Q3PopupMenu* menu = new Q3PopupMenu( parent, name );
00079     KoColorPopupProxy* proxy = 0;
00080 
00081     if ( defaultColor.isValid() ) {
00082         QPixmap pixmap( 12, 12 );
00083         QPainter p( &pixmap );
00084         p.fillRect( 0, 0, 12, 12, defaultColor );
00085         p.end();
00086         proxy = new KoColorPopupProxy( defaultColor, 0, menu, "color proxy" );
00087         connect( proxy, SIGNAL( colorSelected( const QColor& ) ), receiver, slot );
00088         menu->insertItem( QIcon( pixmap ), i18n( "Default Color" ), proxy, SLOT( slotDefaultColor() ) );
00089         menu->insertSeparator();
00090     }
00091 
00092     KoColorPanel* panel = new KoColorPanel( menu, "default colors" );
00093     panel->insertDefaultColors();
00094     connect( panel, SIGNAL( colorSelected( const QColor& ) ), receiver, slot );
00095     menu->insertItem( panel );
00096 
00097     if ( style == CustomColors ) {
00098         menu->insertSeparator();
00099         panel = new KoColorPanel( menu, "custom panel" );
00100         connect( panel, SIGNAL( colorSelected( const QColor& ) ), receiver, slot );
00101         menu->insertItem( panel );
00102         if ( !proxy ) {
00103             proxy = new KoColorPopupProxy( QColor(), panel, menu, "color proxy" );
00104             connect( proxy, SIGNAL( colorSelected( const QColor& ) ), receiver, slot );
00105         }
00106         else
00107             proxy->setRecentColorPanel( panel );
00108         menu->insertSeparator();
00109         menu->insertItem( i18n( "More Colors..." ), proxy, SLOT( slotMoreColors() ) );
00110     }
00111 
00112     return menu;
00113 }
00114 
00115 void KoColorPanel::clear()
00116 {
00117     if ( m_colorMap.isEmpty() )
00118         return;
00119 
00120     QSize area( minimumSizeHint() );
00121     m_colorMap.clear();
00122     init();
00123     updateGeometry();
00124     erase( 0, 0, area.width(), area.height() );
00125 }
00126 
00127 void KoColorPanel::insertColor( const QColor& color )
00128 {
00129     Position pos = m_nextPosition;
00130     if ( insertColor( color, true ) ) // we want checking for external users
00131         finalizeInsertion( pos );
00132 }
00133 
00134 void KoColorPanel::insertColor( const QColor& color, const QString& toolTip )
00135 {
00136     Position pos = m_nextPosition;
00137     if ( insertColor( color, toolTip, true ) ) // we want checking for external users
00138         finalizeInsertion( pos );
00139 }
00140 
00141 void KoColorPanel::insertDefaultColors()
00142 {
00143     if ( m_defaultsAdded )
00144         return;
00145     m_defaultsAdded = true;
00146 
00147     int currentRow = m_nextPosition.y; // we have to repaint this row below
00148 
00149     // Note: No checking for duplicates, so take care when you modify that list
00150     insertColor(QColor( 255 ,     0 ,     0 ), i18nc( "color", "Red" ), false);
00151     insertColor(QColor( 255 ,   165 ,     0 ), i18nc( "color", "Orange" ), false);
00152     insertColor(QColor( 255 ,     0 ,   255 ), i18nc( "color", "Magenta" ), false);
00153     insertColor(QColor(   0 ,     0 ,   255 ), i18nc( "color", "Blue" ), false);
00154     insertColor(QColor(   0 ,   255 ,   255 ), i18nc( "color", "Cyan" ), false);
00155     insertColor(QColor(   0 ,   255 ,     0 ), i18nc( "color", "Green" ), false);
00156     insertColor(QColor( 255 ,   255 ,     0 ), i18nc( "color", "Yellow" ), false);
00157     insertColor(QColor( 165 ,    42 ,    42 ), i18nc( "color", "Brown" ), false);
00158     insertColor(QColor( 139 ,     0 ,     0 ), i18nc( "color", "DarkRed" ), false);
00159     insertColor(QColor( 255 ,   140 ,     0 ), i18nc( "color", "DarkOrange" ), false);
00160     insertColor(QColor( 139 ,     0 ,   139 ), i18nc( "color", "DarkMagenta" ), false);
00161     insertColor(QColor(   0 ,     0 ,   139 ), i18nc( "color", "DarkBlue" ), false);
00162     insertColor(QColor(   0 ,   139 ,   139 ), i18nc( "color", "DarkCyan" ), false);
00163     insertColor(QColor(   0 ,   100 ,     0 ), i18nc( "color", "DarkGreen" ), false);
00164     insertColor(QColor( 130 ,   127 ,     0 ), i18nc( "color", "DarkYellow" ), false);
00165     insertColor(QColor( 255 ,   255 ,   255 ), i18nc( "color", "White" ), false);
00166     // xgettext:no-c-format
00167     insertColor(QColor( 229 ,   229 ,   229 ), i18nc( "color", "Gray 90%" ), false);
00168     // xgettext:no-c-format
00169     insertColor(QColor( 204 ,   204 ,   204 ), i18nc( "color", "Gray 80%" ), false);
00170     // xgettext:no-c-format
00171     insertColor(QColor( 178 ,   178 ,   178 ), i18nc( "color", "Gray 70%" ), false);
00172     // xgettext:no-c-format
00173     insertColor(QColor( 153 ,   153 ,   153 ), i18nc( "color", "Gray 60%" ), false);
00174     // xgettext:no-c-format
00175     insertColor(QColor( 127 ,   127 ,   127 ), i18nc( "color", "Gray 50%" ), false);
00176     // xgettext:no-c-format
00177     insertColor(QColor( 102 ,   102 ,   102 ), i18nc( "color", "Gray 40%" ), false);
00178     // xgettext:no-c-format
00179     insertColor(QColor(  76 ,    76 ,    76 ), i18nc( "color", "Gray 30%" ), false);
00180     // xgettext:no-c-format
00181     insertColor(QColor(  51 ,    51 ,    51 ), i18nc( "color", "Gray 20%" ), false);
00182     // xgettext:no-c-format
00183     insertColor(QColor(  25 ,    25 ,    25 ), i18nc( "color", "Gray 10%" ), false);
00184     insertColor(QColor(   0 ,     0 ,     0 ), i18nc( "color", "Black" ), false);
00185     insertColor(QColor( 255 ,   255 ,   240 ), i18nc( "color", "Ivory" ), false);
00186     insertColor(QColor( 255 ,   250 ,   250 ), i18nc( "color", "Snow" ), false);
00187     insertColor(QColor( 245 ,   255 ,   250 ), i18nc( "color", "MintCream" ), false);
00188     insertColor(QColor( 255 ,   250 ,   240 ), i18nc( "color", "FloralWhite" ), false);
00189     insertColor(QColor( 255 ,   255 ,   224 ), i18nc( "color", "LightYellow" ), false);
00190     insertColor(QColor( 240 ,   255 ,   255 ), i18nc( "color", "Azure" ), false);
00191     insertColor(QColor( 248 ,   248 ,   255 ), i18nc( "color", "GhostWhite" ), false);
00192     insertColor(QColor( 240 ,   255 ,   240 ), i18nc( "color", "Honeydew" ), false);
00193     insertColor(QColor( 255 ,   245 ,   238 ), i18nc( "color", "Seashell" ), false);
00194     insertColor(QColor( 240 ,   248 ,   255 ), i18nc( "color", "AliceBlue" ), false);
00195     insertColor(QColor( 255 ,   248 ,   220 ), i18nc( "color", "Cornsilk" ), false);
00196     insertColor(QColor( 255 ,   240 ,   245 ), i18nc( "color", "LavenderBlush" ), false);
00197     insertColor(QColor( 253 ,   245 ,   230 ), i18nc( "color", "OldLace" ), false);
00198     insertColor(QColor( 245 ,   245 ,   245 ), i18nc( "color", "WhiteSmoke" ), false);
00199     insertColor(QColor( 255 ,   250 ,   205 ), i18nc( "color", "LemonChiffon" ), false);
00200     insertColor(QColor( 224 ,   255 ,   255 ), i18nc( "color", "LightCyan" ), false);
00201     insertColor(QColor( 250 ,   250 ,   210 ), i18nc( "color", "LightGoldenrodYellow" ), false);
00202     insertColor(QColor( 250 ,   240 ,   230 ), i18nc( "color", "Linen" ), false);
00203     insertColor(QColor( 245 ,   245 ,   220 ), i18nc( "color", "Beige" ), false);
00204     insertColor(QColor( 255 ,   239 ,   213 ), i18nc( "color", "PapayaWhip" ), false);
00205     insertColor(QColor( 255 ,   235 ,   205 ), i18nc( "color", "BlanchedAlmond" ), false);
00206     insertColor(QColor( 250 ,   235 ,   215 ), i18nc( "color", "AntiqueWhite" ), false);
00207     insertColor(QColor( 255 ,   228 ,   225 ), i18nc( "color", "MistyRose" ), false);
00208     insertColor(QColor( 230 ,   230 ,   250 ), i18nc( "color", "Lavender" ), false);
00209     insertColor(QColor( 255 ,   228 ,   196 ), i18nc( "color", "Bisque" ), false);
00210     insertColor(QColor( 255 ,   228 ,   181 ), i18nc( "color", "Moccasin" ), false);
00211     insertColor(QColor( 255 ,   222 ,   173 ), i18nc( "color", "NavajoWhite" ), false);
00212     insertColor(QColor( 255 ,   218 ,   185 ), i18nc( "color", "PeachPuff" ), false);
00213     insertColor(QColor( 238 ,   232 ,   170 ), i18nc( "color", "PaleGoldenrod" ), false);
00214     insertColor(QColor( 245 ,   222 ,   179 ), i18nc( "color", "Wheat" ), false);
00215     insertColor(QColor( 220 ,   220 ,   220 ), i18nc( "color", "Gainsboro" ), false);
00216     insertColor(QColor( 240 ,   230 ,   140 ), i18nc( "color", "Khaki" ), false);
00217     insertColor(QColor( 175 ,   238 ,   238 ), i18nc( "color", "PaleTurquoise" ), false);
00218     insertColor(QColor( 255 ,   192 ,   203 ), i18nc( "color", "Pink" ), false);
00219     insertColor(QColor( 238 ,   221 ,   130 ), i18nc( "color", "LightGoldenrod" ), false);
00220     insertColor(QColor( 211 ,   211 ,   211 ), i18nc( "color", "LightGray" ), false);
00221     insertColor(QColor( 255 ,   182 ,   193 ), i18nc( "color", "LightPink" ), false);
00222     insertColor(QColor( 176 ,   224 ,   230 ), i18nc( "color", "PowderBlue" ), false);
00223     insertColor(QColor( 127 ,   255 ,   212 ), i18nc( "color", "Aquamarine" ), false);
00224     insertColor(QColor( 216 ,   191 ,   216 ), i18nc( "color", "Thistle" ), false);
00225     insertColor(QColor( 173 ,   216 ,   230 ), i18nc( "color", "LightBlue" ), false);
00226     insertColor(QColor( 152 ,   251 ,   152 ), i18nc( "color", "PaleGreen" ), false);
00227     insertColor(QColor( 255 ,   215 ,     0 ), i18nc( "color", "Gold" ), false);
00228     insertColor(QColor( 173 ,   255 ,    47 ), i18nc( "color", "GreenYellow" ), false);
00229     insertColor(QColor( 176 ,   196 ,   222 ), i18nc( "color", "LightSteelBlue" ), false);
00230     insertColor(QColor( 144 ,   238 ,   144 ), i18nc( "color", "LightGreen" ), false);
00231     insertColor(QColor( 221 ,   160 ,   221 ), i18nc( "color", "Plum" ), false);
00232     insertColor(QColor( 190 ,   190 ,   190 ), i18nc( "color", "Gray" ), false);
00233     insertColor(QColor( 222 ,   184 ,   135 ), i18nc( "color", "BurlyWood" ), false);
00234     insertColor(QColor( 135 ,   206 ,   250 ), i18nc( "color", "LightSkyBlue" ), false);
00235     insertColor(QColor( 255 ,   160 ,   122 ), i18nc( "color", "LightSalmon" ), false);
00236     insertColor(QColor( 135 ,   206 ,   235 ), i18nc( "color", "SkyBlue" ), false);
00237     insertColor(QColor( 210 ,   180 ,   140 ), i18nc( "color", "Tan" ), false);
00238     insertColor(QColor( 238 ,   130 ,   238 ), i18nc( "color", "Violet" ), false);
00239     insertColor(QColor( 244 ,   164 ,    96 ), i18nc( "color", "SandyBrown" ), false);
00240     insertColor(QColor( 233 ,   150 ,   122 ), i18nc( "color", "DarkSalmon" ), false);
00241     insertColor(QColor( 189 ,   183 ,   107 ), i18nc( "color", "DarkKhaki" ), false);
00242     insertColor(QColor( 127 ,   255 ,     0 ), i18nc( "color", "Chartreuse" ), false);
00243     insertColor(QColor( 169 ,   169 ,   169 ), i18nc( "color", "DarkGray" ), false);
00244     insertColor(QColor( 124 ,   252 ,     0 ), i18nc( "color", "LawnGreen" ), false);
00245     insertColor(QColor( 255 ,   105 ,   180 ), i18nc( "color", "HotPink" ), false);
00246     insertColor(QColor( 250 ,   128 ,   114 ), i18nc( "color", "Salmon" ), false);
00247     insertColor(QColor( 240 ,   128 ,   128 ), i18nc( "color", "LightCoral" ), false);
00248     insertColor(QColor(  64 ,   224 ,   208 ), i18nc( "color", "Turquoise" ), false);
00249     insertColor(QColor( 143 ,   188 ,   143 ), i18nc( "color", "DarkSeaGreen" ), false);
00250     insertColor(QColor( 218 ,   112 ,   214 ), i18nc( "color", "Orchid" ), false);
00251     insertColor(QColor( 102 ,   205 ,   170 ), i18nc( "color", "MediumAquamarine" ), false);
00252     insertColor(QColor( 255 ,   127 ,    80 ), i18nc( "color", "Coral" ), false);
00253     insertColor(QColor( 154 ,   205 ,    50 ), i18nc( "color", "YellowGreen" ), false);
00254     insertColor(QColor( 218 ,   165 ,    32 ), i18nc( "color", "Goldenrod" ), false);
00255     insertColor(QColor(  72 ,   209 ,   204 ), i18nc( "color", "MediumTurquoise" ), false);
00256     insertColor(QColor( 188 ,   143 ,   143 ), i18nc( "color", "RosyBrown" ), false);
00257     insertColor(QColor( 219 ,   112 ,   147 ), i18nc( "color", "PaleVioletRed" ), false);
00258     insertColor(QColor(   0 ,   250 ,   154 ), i18nc( "color", "MediumSpringGreen" ), false);
00259     insertColor(QColor( 255 ,    99 ,    71 ), i18nc( "color", "Tomato" ), false);
00260     insertColor(QColor( 0   ,   255 ,   127 ), i18nc( "color", "SpringGreen" ), false);
00261     insertColor(QColor( 205 ,   133 ,    63 ), i18nc( "color", "Peru" ), false);
00262     insertColor(QColor( 100 ,   149 ,   237 ), i18nc( "color", "CornflowerBlue" ), false);
00263     insertColor(QColor( 132 ,   112 ,   255 ), i18nc( "color", "LightSlateBlue" ), false);
00264     insertColor(QColor( 147 ,   112 ,   219 ), i18nc( "color", "MediumPurple" ), false);
00265     insertColor(QColor( 186 ,    85 ,   211 ), i18nc( "color", "MediumOrchid" ), false);
00266     insertColor(QColor(  95 ,   158 ,   160 ), i18nc( "color", "CadetBlue" ), false);
00267     insertColor(QColor(   0 ,   206 ,   209 ), i18nc( "color", "DarkTurquoise" ), false);
00268     insertColor(QColor(   0 ,   191 ,   255 ), i18nc( "color", "DeepSkyBlue" ), false);
00269     insertColor(QColor( 119 ,   136 ,   153 ), i18nc( "color", "LightSlateGray" ), false);
00270     insertColor(QColor( 184 ,   134 ,    11 ), i18nc( "color", "DarkGoldenrod" ), false);
00271     insertColor(QColor( 123 ,   104 ,   238 ), i18nc( "color", "MediumSlateBlue" ), false);
00272     insertColor(QColor( 205 ,    92 ,    92 ), i18nc( "color", "IndianRed" ), false);
00273     insertColor(QColor( 210 ,   105 ,    30 ), i18nc( "color", "Chocolate" ), false);
00274     insertColor(QColor(  60 ,   179 ,   113 ), i18nc( "color", "MediumSeaGreen" ), false);
00275     insertColor(QColor(  50 ,   205 ,    50 ), i18nc( "color", "LimeGreen" ), false);
00276     insertColor(QColor(  32 ,   178 ,   170 ), i18nc( "color", "LightSeaGreen" ), false);
00277     insertColor(QColor( 112 ,   128 ,   144 ), i18nc( "color", "SlateGray" ), false);
00278     insertColor(QColor(  30 ,   144 ,   255 ), i18nc( "color", "DodgerBlue" ), false);
00279     insertColor(QColor( 255 ,    69 ,     0 ), i18nc( "color", "OrangeRed" ), false);
00280     insertColor(QColor( 255 ,    20 ,   147 ), i18nc( "color", "DeepPink" ), false);
00281     insertColor(QColor(  70 ,   130 ,   180 ), i18nc( "color", "SteelBlue" ), false);
00282     insertColor(QColor( 106 ,    90 ,   205 ), i18nc( "color", "SlateBlue" ), false);
00283     insertColor(QColor( 107 ,   142 ,    35 ), i18nc( "color", "OliveDrab" ), false);
00284     insertColor(QColor(  65 ,   105 ,   225 ), i18nc( "color", "RoyalBlue" ), false);
00285     insertColor(QColor( 208 ,    32 ,   144 ), i18nc( "color", "VioletRed" ), false);
00286     insertColor(QColor( 153 ,    50 ,   204 ), i18nc( "color", "DarkOrchid" ), false);
00287     insertColor(QColor( 160 ,    32 ,   240 ), i18nc( "color", "Purple" ), false);
00288     insertColor(QColor( 105 ,   105 ,   105 ), i18nc( "color", "DimGray" ), false);
00289     insertColor(QColor( 138 ,    43 ,   226 ), i18nc( "color", "BlueViolet" ), false);
00290     insertColor(QColor( 160 ,    82 ,    45 ), i18nc( "color", "Sienna" ), false);
00291     insertColor(QColor( 199 ,    21 ,   133 ), i18nc( "color", "MediumVioletRed" ), false);
00292     insertColor(QColor( 176 ,    48 ,    96 ), i18nc( "color", "Maroon" ), false);
00293     insertColor(QColor(  46 ,   139 ,    87 ), i18nc( "color", "SeaGreen" ), false);
00294     insertColor(QColor(  85 ,   107 ,    47 ), i18nc( "color", "DarkOliveGreen" ), false);
00295     insertColor(QColor(  34 ,   139 ,    34 ), i18nc( "color", "ForestGreen" ), false);
00296     insertColor(QColor( 139 ,    69 ,    19 ), i18nc( "color", "SaddleBrown" ), false);
00297     insertColor(QColor( 148 ,     0 ,   211 ), i18nc( "color", "DarkViolet" ), false);
00298     insertColor(QColor( 178 ,    34 ,    34 ), i18nc( "color", "FireBrick" ), false);
00299     insertColor(QColor(  72 ,    61 ,   139 ), i18nc( "color", "DarkSlateBlue" ), false);
00300     insertColor(QColor(  47 ,    79 ,    79 ), i18nc( "color", "DarkSlateGray" ), false);
00301     insertColor(QColor(  25 ,    25 ,   112 ), i18nc( "color", "MidnightBlue" ), false);
00302     insertColor(QColor(   0 ,     0 ,   205 ), i18nc( "color", "MediumBlue" ), false);
00303     insertColor(QColor(   0 ,     0 ,   128 ), i18nc( "color", "Navy" ), false);
00304 
00305     finalizeInsertion( m_nextPosition );  // with a no-op paint() call as we repaint anyway
00306     updateGeometry();
00307     // we have to repaint the "old" current row explicitly due
00308     // to WStaticContents
00309     update( 0, currentRow << 4, COLS << 4, 16 );
00310 }
00311 
00312 void KoColorPanel::mousePressEvent( QMouseEvent* e )
00313 {
00314     if ( e->button() == Qt::LeftButton )
00315         m_pressedPos = e->pos();
00316 }
00317 
00318 void KoColorPanel::mouseReleaseEvent( QMouseEvent* )
00319 {
00320     if ( isVisible() && parentWidget() && parentWidget()->inherits( "QPopupMenu" ) )
00321         parentWidget()->close();
00322     emit colorSelected( mapToColor( m_pressedPos ) );
00323 }
00324 
00325 void KoColorPanel::mouseMoveEvent( QMouseEvent* e )
00326 {
00327     if ( e->state() & Qt::LeftButton ) {
00328         QPoint p = m_pressedPos - e->pos();
00329         if ( p.manhattanLength() > QApplication::startDragDistance() ) {
00330             QColor color( mapToColor( m_pressedPos ) );
00331             if ( color.isValid() ) {
00332                 K3ColorDrag *drag = new K3ColorDrag( color, this, name() );
00333                 drag->dragCopy();
00334             }
00335         }
00336     }
00337     else
00338         updateFocusPosition( mapToPosition( e->pos() ) );
00339 }
00340 
00341 void KoColorPanel::paintEvent( QPaintEvent* e )
00342 {
00343     int lns = lines();
00344     int startRow, endRow, startCol, endCol;
00345     paintArea( e->rect(), startRow, endRow, startCol, endCol );
00346 
00347     QPainter p( this );
00348 
00349     // First clear all the areas we won't paint on later (only if the widget isn't erased)
00350     if ( !e->erased() ) {
00351         // vertical rects
00352         int tmp = TILESIZE * lns;
00353         if ( startCol == 0 )
00354             erase( 0, 0, 2, tmp );
00355         if ( endCol == COLS )
00356             erase( width() - 2, 0, 2, tmp );
00357         else
00358             erase( ( endCol << 4 ) - 2, 0, 2, tmp );
00359         int i = startCol == 0 ? 1 : startCol;
00360         for ( ; i < endCol; ++i )
00361             erase( ( i << 4 ) - 2, 0, 4, tmp );
00362 
00363         // horizontal rects
00364         tmp = TILESIZE * COLS;
00365         if ( startRow == 0 )
00366             erase( 0, 0, tmp, 2 );
00367         if ( endRow == lns )
00368             erase( 0, height() - 2, tmp, 2 );
00369         else
00370             erase( 0, ( endRow << 4 ) - 2, tmp, 2 );
00371         i = startRow == 0 ? 1 : startRow;
00372         for ( ; i < endRow; ++i )
00373             erase( 0, ( i << 4 ) - 2, tmp, 4 );
00374     }
00375 
00376     // The "active" element (if there is one)
00377     if ( hasFocus() && m_focusPosition.x != -1 && m_focusPosition.y != -1 &&
00378          mapFromPosition( m_focusPosition ).intersects( e->rect() ) )
00379         style().drawPrimitive( QStyle::PE_Panel, &p, QRect( m_focusPosition.x << 4, m_focusPosition.y << 4, TILESIZE, TILESIZE ),
00380                                colorGroup(), QStyle::State_Sunken | QStyle::State_Enabled );
00381 
00382     --lns;  // Attention: We just avoid some lns - 1 statements
00383 
00384     // ...all color tiles
00385     if ( !m_colorMap.isEmpty() ) {
00386         int currentRow = startRow, currentCol = startCol;
00387         while ( currentRow < endRow && currentCol < endCol ) {
00388             QMap<Position, QColor>::ConstIterator it = m_colorMap.find( Position( currentCol, currentRow ) );
00389             if( it != m_colorMap.end() )
00390                 p.fillRect( ( currentCol << 4 ) + 2, ( currentRow << 4 ) + 2, 12, 12, it.data() );
00391 
00392             // position of the next cell
00393             ++currentCol;
00394             if ( currentCol == endCol ) {
00395                 ++currentRow;
00396                 currentCol = startCol;
00397             }
00398         }
00399     }
00400 
00401     // clean up the last line (it's most likely that it's not totally filled)
00402     if ( !e->erased() && endRow > lns ) {
00403         int fields = m_colorMap.count() % COLS;
00404         erase( fields << 4, lns * TILESIZE, ( COLS - fields ) << 4, 16 );
00405     }
00406 }
00407 
00408 void KoColorPanel::keyPressEvent( QKeyEvent* e )
00409 {
00410     Position newPos( validPosition( m_focusPosition ) );
00411     if ( e->key() == Qt::Key_Up ) {
00412         if ( newPos.y == 0 )
00413             e->ignore();
00414         else
00415             --newPos.y;
00416     }
00417     else if ( e->key() == Qt::Key_Down ) {
00418         if ( newPos < Position( m_colorMap.count() % COLS, lines() - 2 ) )
00419             ++newPos.y;
00420         else
00421             e->ignore();
00422     }
00423     else if ( e->key() == Qt::Key_Left ) {
00424         if ( newPos.x == 0 )
00425             e->ignore();
00426         else
00427             --newPos.x;
00428     }
00429     else if ( e->key() == Qt::Key_Right ) {
00430         if ( newPos.x < COLS - 1 && newPos < Position( m_colorMap.count() % COLS - 1, lines() - 1 ) )
00431             ++newPos.x;
00432         else
00433             e->ignore();
00434     }
00435     else if ( e->key() == Qt::Key_Return ) {
00436         if ( isVisible() && parentWidget() && parentWidget()->inherits( "QPopupMenu" ) )
00437             parentWidget()->close();
00438         emit colorSelected( mapToColor( m_focusPosition ) );
00439     }
00440     updateFocusPosition( newPos );
00441 }
00442 
00443 void KoColorPanel::focusInEvent( QFocusEvent* e )
00444 {
00445     if ( !m_colorMap.isEmpty() && m_focusPosition.x == -1 && m_focusPosition.y == -1 ) {
00446         m_focusPosition.x = 0;
00447         m_focusPosition.y = 0;
00448     }
00449     QWidget::focusInEvent( e );
00450 }
00451 
00452 void KoColorPanel::dragEnterEvent( QDragEnterEvent* e )
00453 {
00454     e->accept( K3ColorDrag::canDecode( e ) );
00455 }
00456 
00457 void KoColorPanel::dropEvent( QDropEvent* e )
00458 {
00459     QColor color;
00460     if ( K3ColorDrag::decode( e, color ) )
00461         insertColor( color );
00462 }
00463 
00464 void KoColorPanel::finalizeInsertion( const Position& pos )
00465 {
00466     paint( pos );
00467 
00468     if ( !isFocusEnabled() )
00469         setFocusPolicy( Qt::StrongFocus );
00470     // Did we start a new row?
00471     if ( m_nextPosition.x == 1 )
00472         updateGeometry();
00473 }
00474 
00475 bool KoColorPanel::insertColor( const QColor& color, bool checking )
00476 {
00477     if ( checking && isAvailable( color ) )
00478         return false;
00479 
00480     m_colorMap.insert( m_nextPosition, color );
00481 
00482     ++m_nextPosition.x;
00483     if ( m_nextPosition.x == COLS ) {
00484         m_nextPosition.x = 0;
00485         ++m_nextPosition.y;
00486     }
00487     return true;
00488 }
00489 
00490 bool KoColorPanel::insertColor( const QColor& color, const QString& toolTip, bool checking )
00491 {
00492     if ( checking && isAvailable( color ) )
00493         return false;
00494 
00495     // Remember the "old" m_nextPosition -- this is the place where the newly
00496     // inserted color will be located
00497     QRect rect( mapFromPosition( m_nextPosition ) );
00498     insertColor( color, false ); // check only once ;)
00499     this->setToolTip( rect, toolTip );
00500     return true;
00501 }
00502 
00503 bool KoColorPanel::isAvailable( const QColor& color )
00504 {
00505     // O(n) checking on insert, but this is better than O(n) checking
00506     // on every mouse move...
00507     QMap<Position, QColor>::ConstIterator it = m_colorMap.begin();
00508     QMap<Position, QColor>::ConstIterator end = m_colorMap.end();
00509     for ( ; it != end; ++it )
00510         if ( it.data() == color )
00511             return true;
00512     return false;
00513 }
00514 
00515 KoColorPanel::Position KoColorPanel::mapToPosition( const QPoint& point ) const
00516 {
00517     return Position( point.x() >> 4, point.y() >> 4 );
00518 }
00519 
00520 QColor KoColorPanel::mapToColor( const QPoint& point ) const
00521 {
00522     return mapToColor( mapToPosition( point ) );
00523 }
00524 
00525 QColor KoColorPanel::mapToColor( const KoColorPanel::Position& position ) const
00526 {
00527     QMap<Position, QColor>::ConstIterator it = m_colorMap.find( position );
00528     if ( it != m_colorMap.end() )
00529         return it.data();
00530     return QColor();
00531 }
00532 
00533 QRect KoColorPanel::mapFromPosition( const KoColorPanel::Position& position ) const
00534 {
00535     return QRect( position.x << 4, position.y << 4, TILESIZE, TILESIZE );
00536 }
00537 
00538 KoColorPanel::Position KoColorPanel::validPosition( const Position& position )
00539 {
00540     Position pos( position );
00541     int lns = lines() - 1;
00542     int lastLineLen = m_colorMap.count() % COLS - 1;
00543 
00544     // ensure the position is within the valid grid area
00545     // note: special handling of the last line
00546     if ( pos.x < 0 )
00547         pos.x = 0;
00548     else if ( pos.y == lns && pos.x > lastLineLen )
00549         pos.x = lastLineLen;
00550     else if ( pos.x >= COLS )
00551         pos.x = COLS - 1;
00552 
00553     if ( pos.y < 0 )
00554         pos.y = 0;
00555     else if ( pos.x > lastLineLen && pos.y > lns - 1 )
00556         pos.y = lns - 1;
00557     else if ( pos.y > lns )
00558         pos.y = lns;
00559     return pos;
00560 }
00561 
00562 int KoColorPanel::lines() const
00563 {
00564     if ( m_colorMap.isEmpty() )
00565         return 1;
00566     return ( m_colorMap.count() - 1 ) / COLS + 1;
00567 }
00568 
00569 void KoColorPanel::paintArea( const QRect& rect, int& startRow, int& endRow, int& startCol, int& endCol ) const
00570 {
00571     startRow = rect.top() >> 4;
00572     endRow = ( rect.bottom() >> 4 ) + 1;
00573     startCol = rect.left() >> 4;
00574     endCol = ( rect.right() >> 4 ) + 1;
00575 }
00576 
00577 void KoColorPanel::updateFocusPosition( const Position& newPosition )
00578 {
00579     QPainter p( this );
00580 
00581     // restore the old tile where we had the focus before
00582     if ( m_focusPosition.x != -1 && m_focusPosition.y != -1 )
00583         paint( m_focusPosition );
00584 
00585     m_focusPosition = newPosition;
00586 
00587     QMap<Position, QColor>::ConstIterator it = m_colorMap.find( m_focusPosition );
00588     if ( it != m_colorMap.end() ) {
00589         // draw at the new focus position
00590         style().drawPrimitive( QStyle::PE_Panel, &p, QRect( m_focusPosition.x << 4, m_focusPosition.y << 4, TILESIZE, TILESIZE ),
00591                                colorGroup(), QStyle::State_Sunken | QStyle::State_Enabled );
00592         p.fillRect( ( m_focusPosition.x << 4 ) + 2, ( m_focusPosition.y << 4 ) + 2, 12, 12, it.data() );
00593     }
00594 
00595 }
00596 
00597 void KoColorPanel::paint( const Position& position )
00598 {
00599     QMap<Position, QColor>::ConstIterator it = m_colorMap.find( position );
00600     if ( it == m_colorMap.end() )
00601         return;
00602 
00603     erase( mapFromPosition( position ) );
00604     QPainter p( this );
00605     p.fillRect( ( position.x << 4 ) + 2, ( position.y << 4 ) + 2, 12, 12, it.data() );
00606 }
00607 
00608 void KoColorPanel::init()
00609 {
00610     setFocusPolicy( Qt::NoFocus ); // it's empty
00611     m_nextPosition.x = 0;
00612     m_nextPosition.y = 0;
00613     m_focusPosition.x = -1;
00614     m_focusPosition.y = -1;
00615     m_defaultsAdded = false;
00616 }
00617 
00618 bool operator<( const KoColorPanel::Position& lhs, const KoColorPanel::Position& rhs )
00619 {
00620     return ( lhs.y * COLS + lhs.x ) < ( rhs.y * COLS + rhs.x );
00621 }
00622 
00623 
00624 KoColorPopupProxy::KoColorPopupProxy( const QColor& defaultColor, KoColorPanel* recentColors, QObject* parent, const char* name ) :
00625     QObject( parent, name ), m_defaultColor( defaultColor ), m_recentColors( recentColors )
00626 {
00627 }
00628 
00629 void KoColorPopupProxy::setRecentColorPanel( KoColorPanel* recentColors )
00630 {
00631     m_recentColors = recentColors;
00632 }
00633 
00634 void KoColorPopupProxy::slotDefaultColor()
00635 {
00636     emit colorSelected( m_defaultColor );
00637 }
00638 
00639 void KoColorPopupProxy::slotMoreColors()
00640 {
00641     if ( !m_recentColors )
00642         return;
00643 
00644     QColor newColor;
00645     QWidget* p = 0;
00646     if ( parent() && parent()->isWidgetType() )
00647         p = static_cast<QWidget*>( parent() );
00648 
00649     if ( KColorDialog::getColor( newColor, p ) == QDialog::Accepted ) {
00650         m_recentColors->insertColor( newColor );
00651         emit colorSelected( newColor );
00652     }
00653 }
00654 
00655 
00656 KoToolButton::KoToolButton( const QString& icon, int id, QWidget* parent,
00657                             const char* name, const QString& txt, KInstance* _instance ) :
00658     KToolBarButton( icon, id, parent, name, txt, _instance ), m_arrowPressed( false )
00659 {
00660     init();
00661 }
00662 
00663 KoToolButton::KoToolButton( const QPixmap& pixmap, int id, QWidget* parent,
00664                             const char* name, const QString& txt ) :
00665     KToolBarButton( pixmap, id, parent, name, txt ), m_arrowPressed( false )
00666 {
00667     init();
00668 }
00669 
00670 KoToolButton::~KoToolButton()
00671 {
00672 }
00673 
00674 QSize KoToolButton::sizeHint() const
00675 {
00676     return minimumSizeHint();
00677 }
00678 
00679 QSize KoToolButton::minimumSizeHint() const
00680 {
00681     QSize size = KToolBarButton::minimumSizeHint();
00682     size.setWidth( size.width() + ARROW_WIDTH );
00683     return size;
00684 }
00685 
00686 QSize KoToolButton::minimumSize() const
00687 {
00688     return minimumSizeHint();
00689 }
00690 
00691 void KoToolButton::colorSelected( const QColor& color )
00692 {
00693     kDebug() << "selected::: " << color.name() << endl;
00694 }
00695 
00696 void KoToolButton::drawButton(QPainter *_painter)
00697 {
00698     QStyle::State flags = QStyle::State_None;
00699     QStyle::SubControls active = QStyle::SC_None;
00700     QStyle::SubControls arrowActive = QStyle::SC_None;
00701     QStyleOption opt;
00702     QColorGroup cg( colorGroup() );
00703 
00704     if ( isEnabled() ) {
00705         flags |= QStyle::State_Enabled;
00706         if ( KToolBarButton::isRaised() || m_arrowPressed )
00707             flags |= QStyle::State_Raised;
00708     }
00709     if ( isOn() )
00710         flags |= QStyle::State_On;
00711 
00712     QStyle::State arrowFlags = flags;
00713 
00714     if ( isDown() && !m_arrowPressed ) {
00715         flags  |= QStyle::State_DownArrow;
00716         active |= QStyle::SC_ToolButton;
00717     }
00718     if ( m_arrowPressed )
00719         arrowActive |= QStyle::SC_ToolButton;
00720 
00721     // Draw styled toolbuttons
00722     _painter->setClipRect( 0, 0, width() - ARROW_WIDTH, height() );
00723     style().drawComplexControl( QStyle::CC_ToolButton, _painter, this, QRect( 0, 0, width() - ARROW_WIDTH, height() ), cg,
00724                                 flags, QStyle::SC_ToolButton, active, opt );
00725     _painter->setClipRect( width() - ARROW_WIDTH, 0, ARROW_WIDTH, height() );
00726     style().drawComplexControl( QStyle::CC_ToolButton, _painter, this, QRect( width(), 0, ARROW_WIDTH, height() ), cg,
00727                                 arrowFlags, QStyle::SC_ToolButton, arrowActive, opt );
00728     _painter->setClipping( false );
00729 
00730     // ...and the arrow indicating the popup
00731     style().drawPrimitive( QStyle::PE_ArrowDown, _painter, QRect( width() - ARROW_WIDTH - 1, 0, ARROW_WIDTH, height() ),
00732                            cg, flags, opt );
00733 
00734     if ( KToolBarButton::isRaised() || m_arrowPressed )
00735         qDrawShadeLine( _painter, width() - ARROW_WIDTH - 1, 0, width() - ARROW_WIDTH - 1, height() - 1, colorGroup(), true );
00736 
00737     int dx, dy;
00738     QFont tmp_font( KGlobalSettings::toolBarFont() );
00739     QFontMetrics fm( tmp_font );
00740     QRect textRect;
00741     int textFlags = 0;
00742 
00743     if ( static_cast<KToolBar::IconText>( iconTextMode() ) == KToolBar::IconOnly ) { // icon only
00744         QPixmap pixmap = iconSet().pixmap( QIcon::Automatic,
00745                                            isEnabled() ? ( KToolBarButton::isActive() ? QIcon::Active : QIcon::Normal ) :
00746                                            QIcon::Disabled, isOn() ? QIcon::On : QIcon::Off );
00747         if ( !pixmap.isNull() ) {
00748             dx = ( width() - ARROW_WIDTH - pixmap.width() ) / 2;
00749             dy = ( height() - pixmap.height() ) / 2;
00750             buttonShift( dx, dy );
00751             _painter->drawPixmap( dx, dy, pixmap );
00752         }
00753     }
00754     else if ( static_cast<KToolBar::IconText>( iconTextMode() ) == KToolBar::IconTextRight ) { // icon and text (if any)
00755         QPixmap pixmap = iconSet().pixmap( QIcon::Automatic,
00756                                            isEnabled() ? ( KToolBarButton::isActive() ? QIcon::Active : QIcon::Normal ) :
00757                                            QIcon::Disabled, isOn() ? QIcon::On : QIcon::Off );
00758         if( !pixmap.isNull()) {
00759             dx = 4;
00760             dy = ( height() - pixmap.height() ) / 2;
00761             buttonShift( dx, dy );
00762             _painter->drawPixmap( dx, dy, pixmap );
00763         }
00764 
00765         if (!textLabel().isNull()) {
00766             textFlags = Qt::AlignVCenter | Qt::AlignLeft;
00767             if ( !pixmap.isNull() )
00768                 dx = 4 + pixmap.width() + 2;
00769             else
00770                 dx = 4;
00771             dy = 0;
00772             buttonShift( dx, dy );
00773             textRect = QRect( dx, dy, width() - dx, height() );
00774         }
00775     }
00776     else if ( static_cast<KToolBar::IconText>( iconTextMode() ) == KToolBar::TextOnly ) {
00777         if ( !textLabel().isNull() ) {
00778             textFlags = Qt::AlignTop | Qt::AlignLeft;
00779             dx = ( width() - ARROW_WIDTH - fm.width( textLabel() ) ) / 2;
00780             dy = ( height() - fm.lineSpacing() ) / 2;
00781             buttonShift( dx, dy );
00782             textRect = QRect( dx, dy, fm.width(textLabel()), fm.lineSpacing() );
00783         }
00784     }
00785     else if ( static_cast<KToolBar::IconText>( iconTextMode() ) == KToolBar::IconTextBottom ) {
00786         QPixmap pixmap = iconSet().pixmap( QIcon::Automatic,
00787                                            isEnabled() ? ( KToolBarButton::isActive() ? QIcon::Active : QIcon::Normal ) :
00788                                            QIcon::Disabled, isOn() ? QIcon::On : QIcon::Off );
00789         if( !pixmap.isNull()) {
00790             dx = ( width() - ARROW_WIDTH - pixmap.width() ) / 2;
00791             dy = ( height() - fm.lineSpacing() - pixmap.height() ) / 2;
00792             buttonShift( dx, dy );
00793             _painter->drawPixmap( dx, dy, pixmap );
00794         }
00795 
00796         if ( !textLabel().isNull() ) {
00797             textFlags = Qt::AlignBottom | Qt::AlignHCenter;
00798             dx = ( width() - ARROW_WIDTH - fm.width( textLabel() ) ) / 2;
00799             dy = height() - fm.lineSpacing() - 4;
00800             buttonShift( dx, dy );
00801             textRect = QRect( dx, dy, fm.width( textLabel() ), fm.lineSpacing() );
00802         }
00803     }
00804 
00805     // Draw the text at the position given by textRect, and using textFlags
00806     if (!textLabel().isNull() && !textRect.isNull()) {
00807         _painter->setFont( KGlobalSettings::toolBarFont() );
00808         if ( !isEnabled() )
00809             _painter->setPen( palette().disabled().dark() );
00810         else if( KToolBarButton::isRaised() )
00811             _painter->setPen( KGlobalSettings::toolBarHighlightColor() );
00812         else
00813             _painter->setPen( colorGroup().buttonText() );
00814         _painter->drawText( textRect, textFlags, textLabel() );
00815     }
00816 }
00817 
00818 bool KoToolButton::eventFilter( QObject* o, QEvent* e )
00819 {
00820     if ( o == m_popup ) {
00821         if ( e->type() == QEvent::MouseButtonPress )
00822             if ( hitArrow( mapFromGlobal( static_cast<QMouseEvent*>( e )->globalPos() ) ) ) {
00823                 kDebug() << "KoToolButton::eventFilter-------------->" << endl;
00824                 m_popup->close();
00825                 m_arrowPressed = false;
00826                 return true;
00827             }
00828         return false;
00829     }
00830 
00831     if ( e->type() == QEvent::MouseButtonPress ) {
00832         m_arrowPressed = hitArrow( static_cast<QMouseEvent*>( e )->pos() );
00833         if ( m_arrowPressed )
00834             m_popup->popup( mapToGlobal( QPoint( 0, height() ) ) );
00835     }
00836     else if ( e->type() == QEvent::MouseButtonRelease )
00837         m_arrowPressed = false;
00838     return KToolBarButton::eventFilter( o, e );
00839 }
00840 
00841 void KoToolButton::init()
00842 {
00843     m_popup = KoColorPanel::createColorPopup( KoColorPanel::CustomColors, Qt::yellow, this,
00844                                               SLOT( colorSelected( const QColor& ) ),
00845                                               this, "no-name" );
00846     // We are interested in the mouse clicks on the arrow button
00847     m_popup->installEventFilter( this );
00848 
00849     ARROW_WIDTH = style().pixelMetric(QStyle::PM_MenuButtonIndicator) + 4;
00850     kDebug() << "##################### Arrow: " << ARROW_WIDTH << endl;
00851 }
00852 
00853 void KoToolButton::buttonShift( int& dx, int& dy )
00854 {
00855     if ( isDown() && !m_arrowPressed ) {
00856         dx += style().pixelMetric( QStyle::PM_ButtonShiftHorizontal );
00857         dy += style().pixelMetric( QStyle::PM_ButtonShiftVertical );
00858     }
00859 }
00860 
00861 bool KoToolButton::hitArrow( const QPoint& pos )
00862 {
00863     return QRect( width() - ARROW_WIDTH, 0, ARROW_WIDTH, height() ).contains( pos );
00864 }
00865 
00866 #include <KoTooluButton.moc>

Généré le Wed Nov 22 23:41:05 2006 pour KPlato par  doxygen 1.5.1-p1