krita/ui

kis_cursor.cc

Go to the documentation of this file.
00001 /*
00002  *  kis_cursor.cc - part of KImageShop
00003  *
00004  *  Copyright (c) 1999 Matthias Elter <elter@kde.org>
00005  *  Copyright (c) 2004 Adrian Page <adrian@pagenet.plus.com>
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; either version 2 of the License, or
00010  *  (at your option) any later version.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  */
00021 
00022 #include "kis_cursor.h"
00023 
00024 
00025 #include <QBitmap>
00026 #include <QCursor>
00027 #include <QImage>
00028 #include <QPainter>
00029 
00030 #include <kcursor.h>
00031 #include <kiconloader.h>
00032 #include <kcomponentdata.h>
00033 #include <kstandarddirs.h>
00034 
00035 #include "kis_factory2.h"
00036 
00037 KisCursor::KisCursor() {}
00038 
00039 /*
00040  * Predefined Qt cursors
00041  */
00042 QCursor KisCursor::arrowCursor()
00043 {
00044     return Qt::ArrowCursor;
00045 }
00046 
00047 QCursor KisCursor::upArrowCursor()
00048 {
00049     return Qt::UpArrowCursor;
00050 }
00051 
00052 QCursor KisCursor::crossCursor()
00053 {
00054     return Qt::CrossCursor;
00055 }
00056 
00057 QCursor KisCursor::waitCursor()
00058 {
00059     return Qt::WaitCursor;
00060 }
00061 
00062 QCursor KisCursor::ibeamCursor()
00063 {
00064     return Qt::IBeamCursor;
00065 }
00066 
00067 QCursor KisCursor::sizeVerCursor()
00068 {
00069     return Qt::SizeVerCursor;
00070 }
00071 
00072 QCursor KisCursor::sizeHorCursor()
00073 {
00074     return Qt::SizeHorCursor;
00075 }
00076 
00077 QCursor KisCursor::sizeBDiagCursor()
00078 {
00079     return Qt::SizeBDiagCursor;
00080 }
00081 
00082 QCursor KisCursor::sizeFDiagCursor()
00083 {
00084     return Qt::SizeFDiagCursor;
00085 }
00086 
00087 QCursor KisCursor::sizeAllCursor()
00088 {
00089     return Qt::SizeAllCursor;
00090 }
00091 
00092 QCursor KisCursor::blankCursor()
00093 {
00094     return Qt::BlankCursor;
00095 }
00096 
00097 QCursor KisCursor::splitVCursor()
00098 {
00099     return Qt::SplitVCursor;
00100 }
00101 
00102 QCursor KisCursor::splitHCursor()
00103 {
00104     return Qt::SplitHCursor;
00105 }
00106 
00107 QCursor KisCursor::pointingHandCursor()
00108 {
00109     return Qt::PointingHandCursor;
00110 }
00111 
00112 
00113 /*
00114  * Existing custom KimageShop cursors. Use the 'load' function for all new cursors.
00115  */
00116 
00117 QCursor KisCursor::pickerCursor()
00118 {
00119     static unsigned char picker_bits[] = {
00120         0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x34, 0x00, 0x00, 0x7a,
00121         0x00, 0x00, 0x7d, 0x00, 0x80, 0x7e, 0x00, 0x60, 0x3f, 0x00, 0xd0, 0x1f,
00122         0x00, 0xa0, 0x0f, 0x00, 0x50, 0x07, 0x00, 0xc8, 0x06, 0x00, 0xe4, 0x02,
00123         0x00, 0x72, 0x01, 0x00, 0x39, 0x00, 0x80, 0x1c, 0x00, 0x40, 0x0e, 0x00,
00124         0x20, 0x07, 0x00, 0x90, 0x03, 0x00, 0xc8, 0x01, 0x00, 0xe4, 0x00, 0x00,
00125         0x74, 0x00, 0x00, 0x32, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00
00126     };
00127 
00128     QBitmap picker_bitmap = QBitmap::fromData(QSize(24, 24), picker_bits);
00129     QBitmap picker_mask = picker_bitmap.createHeuristicMask(false);
00130 
00131     return QCursor(picker_bitmap, picker_mask, 1, 22);
00132 }
00133 
00134 
00135 QCursor KisCursor::pickerPlusCursor()
00136 {
00137     static unsigned char pickerplus_bits[] = {
00138         0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x34, 0x00, 0x00, 0x7a,
00139         0x00, 0x00, 0x7d, 0x00, 0x80, 0x7e, 0x00, 0x60, 0x3f, 0x00, 0xd0, 0x1f,
00140         0x00, 0xa0, 0x0f, 0x00, 0x50, 0x07, 0x00, 0xc8, 0x06, 0x00, 0xe4, 0x02,
00141         0x00, 0x72, 0x01, 0x00, 0x39, 0x0c, 0x80, 0x1c, 0x0c, 0x40, 0x0e, 0x0c,
00142         0x20, 0x07, 0x0c, 0x90, 0x83, 0x7f, 0xc8, 0x81, 0x7f, 0xe4, 0x00, 0x0c,
00143         0x74, 0x00, 0x0c, 0x32, 0x00, 0x0c, 0x0a, 0x00, 0x0c, 0x00, 0x00, 0x00
00144     };
00145 
00146     QBitmap picker_bitmap = QBitmap::fromData(QSize(24, 24), pickerplus_bits);
00147     QBitmap picker_mask = picker_bitmap.createHeuristicMask(false);
00148 
00149     return QCursor(picker_bitmap, picker_mask, 1, 22);
00150 }
00151 
00152 
00153 QCursor KisCursor::pickerMinusCursor()
00154 {
00155     static unsigned char pickerminus_bits[] = {
00156         0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x34, 0x00, 0x00, 0x7a,
00157         0x00, 0x00, 0x7d, 0x00, 0x80, 0x7e, 0x00, 0x60, 0x3f, 0x00, 0xd0, 0x1f,
00158         0x00, 0xa0, 0x0f, 0x00, 0x50, 0x07, 0x00, 0xc8, 0x06, 0x00, 0xe4, 0x02,
00159         0x00, 0x72, 0x01, 0x00, 0x39, 0x00, 0x80, 0x1c, 0x00, 0x40, 0x0e, 0x00,
00160         0x20, 0x07, 0x00, 0x90, 0xc3, 0x7f, 0xc8, 0xc1, 0x7f, 0xe4, 0x00, 0x00,
00161         0x74, 0x00, 0x00, 0x32, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00
00162     };
00163 
00164     QBitmap picker_bitmap = QBitmap::fromData(QSize(24, 24), pickerminus_bits);
00165     QBitmap picker_mask = picker_bitmap.createHeuristicMask(false);
00166 
00167     return QCursor(picker_bitmap, picker_mask, 1, 22);
00168 }
00169 QCursor KisCursor::penCursor()
00170 {
00171     static unsigned char pen_bits[] = {
00172         0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x3a, 0x00, 0x00, 0x7d,
00173         0x00, 0x80, 0x7e, 0x00, 0x40, 0x7f, 0x00, 0xa0, 0x3f, 0x00, 0xd0, 0x1f,
00174         0x00, 0xe8, 0x0f, 0x00, 0xf4, 0x07, 0x00, 0xfa, 0x03, 0x00, 0xfd, 0x01,
00175         0x80, 0xfe, 0x00, 0x40, 0x7f, 0x00, 0xa0, 0x3f, 0x00, 0xf0, 0x1f, 0x00,
00176         0xd0, 0x0f, 0x00, 0x88, 0x07, 0x00, 0x88, 0x03, 0x00, 0xe4, 0x01, 0x00,
00177         0x7c, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00
00178     };
00179 
00180     QBitmap pen_bitmap = QBitmap::fromData(QSize(24, 24), pen_bits);
00181     QBitmap pen_mask = pen_bitmap.createHeuristicMask(false);
00182 
00183     return QCursor(pen_bitmap, pen_mask, 1, 22);
00184 }
00185 
00186 QCursor KisCursor::brushCursor()
00187 {
00188     static unsigned char brush_bits[] = {
00189         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x68, 0x00,
00190         0x00, 0x00, 0xf4, 0x00, 0x00, 0x00, 0xfa, 0x00, 0x00, 0x00, 0xfd, 0x00,
00191         0x00, 0x80, 0x7e, 0x00, 0x00, 0x40, 0x3f, 0x00, 0x00, 0xa0, 0x1f, 0x00,
00192         0x00, 0xd0, 0x0f, 0x00, 0x00, 0xe8, 0x07, 0x00, 0x00, 0xf4, 0x03, 0x00,
00193         0x00, 0xe4, 0x01, 0x00, 0x00, 0xc2, 0x00, 0x00, 0x80, 0x41, 0x00, 0x00,
00194         0x40, 0x32, 0x00, 0x00, 0xa0, 0x0f, 0x00, 0x00, 0xd0, 0x0f, 0x00, 0x00,
00195         0xd0, 0x0f, 0x00, 0x00, 0xe8, 0x07, 0x00, 0x00, 0xf4, 0x01, 0x00, 0x00,
00196         0x7e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
00197     };
00198 
00199     QBitmap brush_bitmap = QBitmap::fromData(QSize(25, 23), brush_bits);
00200     QBitmap brush_mask = brush_bitmap.createHeuristicMask(false);
00201 
00202     return QCursor(brush_bitmap, brush_mask, 1, 21);
00203 }
00204 
00205 QCursor KisCursor::airbrushCursor()
00206 {
00207     static unsigned char airbrush_bits[] = {
00208         0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x68, 0x00, 0x00, 0x74,
00209         0x00, 0x00, 0x7a, 0xf0, 0x00, 0x3d, 0x08, 0x81, 0x1e, 0xe8, 0x41, 0x0f,
00210         0xe8, 0xa1, 0x07, 0xe8, 0xd1, 0x03, 0xe8, 0xe9, 0x01, 0xe8, 0xf5, 0x00,
00211         0xe8, 0x7b, 0x00, 0xf0, 0x33, 0x00, 0xf0, 0x23, 0x1f, 0xa0, 0x9f, 0x3f,
00212         0xd0, 0xff, 0x31, 0xe8, 0xf7, 0x30, 0xf4, 0x03, 0x18, 0xfc, 0x01, 0x0c,
00213         0xf8, 0x00, 0x06, 0x76, 0x00, 0x03, 0x36, 0x00, 0x03, 0x00, 0x00, 0x00
00214     };
00215 
00216     QBitmap airbrush_bitmap = QBitmap::fromData(QSize(24, 24), airbrush_bits);
00217     QBitmap airbrush_mask = airbrush_bitmap.createHeuristicMask(false);
00218 
00219     return QCursor(airbrush_bitmap, airbrush_mask, 1, 22);
00220 }
00221 
00222 QCursor KisCursor::eraserCursor()
00223 {
00224     static unsigned char eraser_bits[] = {
00225         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x1d, 0x00,
00226         0x00, 0x80, 0x3e, 0x00, 0x00, 0x40, 0x7f, 0x00, 0x00, 0xa0, 0xff, 0x00,
00227         0x00, 0xd0, 0xff, 0x00, 0x00, 0xe8, 0x7f, 0x00, 0x00, 0xf4, 0x3f, 0x00,
00228         0x00, 0xfe, 0x1f, 0x00, 0x00, 0xf9, 0x0f, 0x00, 0x80, 0xf2, 0x07, 0x00,
00229         0x40, 0xe7, 0x03, 0x00, 0xa0, 0xcf, 0x01, 0x00, 0xd0, 0x9f, 0x00, 0x00,
00230         0xe8, 0x7f, 0x00, 0x00, 0xfc, 0x3f, 0x00, 0x00, 0xf2, 0x1f, 0x00, 0x00,
00231         0xe2, 0x0f, 0x00, 0x00, 0xc4, 0x07, 0x00, 0x00, 0x88, 0x03, 0x00, 0x00,
00232         0x10, 0x01, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
00233     };
00234 
00235     QBitmap eraser_bitmap = QBitmap::fromData(QSize(25, 24), eraser_bits);
00236     QBitmap eraser_mask = eraser_bitmap.createHeuristicMask(false);
00237 
00238     return QCursor(eraser_bitmap, eraser_mask, 7, 22);
00239 }
00240 
00241 QCursor KisCursor::fillerCursor()
00242 {
00243     static unsigned char filler_bits[] = {
00244         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x28, 0x00,
00245         0x00, 0x54, 0x00, 0x00, 0x4e, 0x00, 0x00, 0x85, 0x00, 0x80, 0x0a, 0x01,
00246         0x40, 0x11, 0x01, 0xe0, 0x00, 0x02, 0x58, 0x01, 0x04, 0x2c, 0x02, 0x04,
00247         0x44, 0x04, 0x08, 0x0c, 0x08, 0x18, 0x3c, 0x00, 0x14, 0x5c, 0x00, 0x0a,
00248         0x9c, 0x01, 0x05, 0x1c, 0x82, 0x02, 0x18, 0x4c, 0x01, 0x18, 0xb0, 0x00,
00249         0x08, 0x60, 0x00, 0x00, 0x00, 0x00
00250     };
00251 
00252     QBitmap filler_bitmap = QBitmap::fromData(QSize(22, 22), filler_bits);
00253     QBitmap filler_mask = filler_bitmap.createHeuristicMask(false);
00254 
00255     return QCursor(filler_bitmap, filler_mask, 3, 20);
00256 }
00257 
00258 QCursor KisCursor::colorChangerCursor()
00259 {
00260     static unsigned char colorChanger_bits[] = {
00261         0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x10, 0x01, 0x0e, 0x08, 0x02, 0x11,
00262         0x04, 0x82, 0x20, 0x64, 0x84, 0x20, 0x92, 0x44, 0x46, 0x12, 0x49, 0x5f,
00263         0x12, 0x31, 0x5f, 0x22, 0x01, 0x5f, 0xc2, 0x00, 0x4e, 0x02, 0x00, 0x40,
00264         0xc2, 0x00, 0x46, 0xe2, 0x01, 0x4f, 0xe4, 0x19, 0x2f, 0xe4, 0x3d, 0x2f,
00265         0xe8, 0x3d, 0x17, 0xd0, 0x3c, 0x10, 0x20, 0x38, 0x08, 0x40, 0x00, 0x06,
00266         0x80, 0x81, 0x01, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x00
00267     };
00268 
00269     QBitmap colorChanger_bitmap = QBitmap::fromData(QSize(24, 23), colorChanger_bits);
00270     QBitmap colorChanger_mask = colorChanger_bitmap.createHeuristicMask(false);
00271 
00272     return QCursor(colorChanger_bitmap, colorChanger_mask, 12, 10);
00273 }
00274 
00275 QCursor KisCursor::zoomCursor()
00276 {
00277     static unsigned char zoom_bits[] = {
00278         0x00, 0x00, 0x00, 0xc0, 0x0f, 0x00, 0xf0, 0x3f, 0x00, 0x38, 0x70, 0x00,
00279         0x8c, 0xcf, 0x00, 0x0c, 0xdf, 0x00, 0x36, 0xbf, 0x01, 0xb6, 0xbf, 0x01,
00280         0xf6, 0xbf, 0x01, 0xf6, 0xbf, 0x01, 0xe6, 0x9f, 0x00, 0xcc, 0xcf, 0x00,
00281         0x9c, 0xe7, 0x01, 0x38, 0x70, 0x03, 0xf0, 0xbf, 0x05, 0xc0, 0xef, 0x0b,
00282         0x00, 0xc0, 0x17, 0x00, 0x80, 0x2f, 0x00, 0x00, 0x5f, 0x00, 0x00, 0x7e,
00283         0x00, 0x00, 0x7c, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00
00284     };
00285 
00286     QBitmap zoom_bitmap = QBitmap::fromData(QSize(24, 23), zoom_bits);
00287     QBitmap zoom_mask = zoom_bitmap.createHeuristicMask(false);
00288 
00289     return QCursor(zoom_bitmap, zoom_mask, 9, 8);
00290 }
00291 
00292 QCursor KisCursor::moveCursor()
00293 {
00294     static unsigned char move_bits[] = {
00295         0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x7e, 0x00,
00296         0x00, 0xff, 0x00, 0x00, 0x18, 0x00, 0x00, 0x18, 0x00, 0x00, 0x18, 0x00,
00297         0x10, 0x18, 0x08, 0x18, 0x18, 0x18, 0x1c, 0x18, 0x38, 0xfe, 0xff, 0x7f,
00298         0xfe, 0xff, 0x7f, 0x1c, 0x18, 0x38, 0x18, 0x18, 0x18, 0x10, 0x18, 0x08,
00299         0x00, 0x18, 0x00, 0x00, 0x18, 0x00, 0x00, 0x18, 0x00, 0x00, 0xff, 0x00,
00300         0x00, 0x7e, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00
00301     };
00302 
00303     QBitmap move_bitmap = QBitmap::fromData(QSize(24, 24), move_bits);
00304     QBitmap move_mask = move_bitmap.createHeuristicMask(false);
00305 
00306     return QCursor(move_bitmap, move_mask, 12, 11);
00307 }
00308 
00309 QCursor KisCursor::handCursor()
00310 {
00311     return Qt::PointingHandCursor;
00312 }
00313 
00314 QCursor KisCursor::selectCursor()
00315 {
00316     static unsigned char select_bits[] = {
00317         0x00, 0x08, 0x00, 0x00, 0x08, 0x00, 0x00, 0x08, 0x00, 0x00, 0x08, 0x00,
00318         0x00, 0x08, 0x00, 0x00, 0x08, 0x00, 0x00, 0x08, 0x00, 0x00, 0x08, 0x00,
00319         0x00, 0x08, 0x00, 0x00, 0x08, 0x00, 0x00, 0x08, 0x00, 0xff, 0xff, 0x7f,
00320         0x00, 0x08, 0x00, 0x00, 0x08, 0x00, 0x00, 0x08, 0x00, 0x00, 0x08, 0x00,
00321         0x00, 0x08, 0x00, 0x00, 0x08, 0x00, 0x00, 0x08, 0x00, 0x00, 0x08, 0x00,
00322         0x00, 0x08, 0x00, 0x00, 0x08, 0x00, 0x00, 0x08, 0x00
00323     };
00324 
00325     QBitmap select_bitmap = QBitmap::fromData(QSize(23, 23), select_bits);
00326     QBitmap select_mask = select_bitmap.createHeuristicMask(false);
00327 
00328     return QCursor(select_bitmap, select_mask, 11, 11);
00329 }
00330 
00331 QCursor KisCursor::openHandCursor()
00332 {
00333     return load("openhand_cursor.xpm");
00334 }
00335 
00336 QCursor KisCursor::closedHandCursor()
00337 {
00338     return load("closedhand_cursor.xpm");
00339 }
00340 
00341 QCursor KisCursor::rotateCursor()
00342 {
00343     return load("rotate_cursor.xpm");
00344 }
00345 
00346 QCursor KisCursor::load(const QString & iconName, int hotspotX, int hotspotY)
00347 {
00348     QString filename = KisFactory2::componentData().dirs()->findResource("kis_pics", iconName);
00349     QImage cursorImage;
00350 
00351     cursorImage.load(filename);
00352     Q_ASSERT(!cursorImage.isNull());
00353     Q_ASSERT(cursorImage.hasAlphaChannel());
00354 
00355     QBitmap bitmap(cursorImage.width(), cursorImage.height());
00356     QBitmap mask(cursorImage.width(), cursorImage.height());
00357 
00358     QPainter bitmapPainter(&bitmap);
00359     QPainter maskPainter(&mask);
00360 
00361     for (qint32 x = 0; x < cursorImage.width(); ++x) {
00362         for (qint32 y = 0; y < cursorImage.height(); ++y) {
00363 
00364             QRgb pixel = cursorImage.pixel(x, y);
00365 
00366             if (qAlpha(pixel) < 128) {
00367                 bitmapPainter.setPen(Qt::color0);
00368                 maskPainter.setPen(Qt::color0);
00369             } else {
00370                 maskPainter.setPen(Qt::color1);
00371 
00372                 if (qGray(pixel) < 128) {
00373                     bitmapPainter.setPen(Qt::color1);
00374                 } else {
00375                     bitmapPainter.setPen(Qt::color0);
00376                 }
00377             }
00378 
00379             bitmapPainter.drawPoint(x, y);
00380             maskPainter.drawPoint(x, y);
00381         }
00382     }
00383 
00384     return QCursor(bitmap, mask, hotspotX, hotspotY);
00385 }
00386