• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

KStyles

tileset.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright 2008 Long Huynh Huu <long.upcase@googlemail.com>
00003  * Copyright 2007 Matthew Woehlke <mw_triad@users.sourceforge.net>
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Library General Public
00007  * License version 2 as published by the Free Software Foundation.
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 <QtGui/QPainter>
00021 
00022 #include "tileset.h"
00023 
00024 void TileSet::initPixmap(int s, const QPixmap &pix, int w, int h, const QRect &region)
00025 {
00026     if (w != region.width() || h != region.height()) {
00027         QPixmap tile = pix.copy(region);
00028         _pixmap[s] = QPixmap(w, h);
00029         _pixmap[s].fill(QColor(0,0,0,0));
00030         QPainter p(&_pixmap[s]);
00031         p.drawTiledPixmap(0, 0, w, h, tile);
00032     }
00033     else {
00034         _pixmap[s] = pix.copy(region);
00035     }
00036 }
00037 
00038 TileSet::TileSet(const QPixmap &pix, int w1, int h1, int w2, int h2)
00039     : _w1(w1), _h1(h1)
00040 {
00041     if (pix.isNull())
00042         return;
00043 
00044     _w3 = pix.width() - (w1 + w2);
00045     _h3 = pix.height() - (h1 + h2);
00046     int w = w2; while (w < 32 && w2 > 0) w += w2;
00047     int h = h2; while (h < 32 && h2 > 0) h += h2;
00048 
00049     initPixmap(0, pix, _w1, _h1, QRect(0,      0,      _w1, _h1));
00050     initPixmap(1, pix,  w,  _h1, QRect(_w1,    0,       w2, _h1));
00051     initPixmap(2, pix, _w3, _h1, QRect(_w1+w2, 0,      _w3, _h1));
00052     initPixmap(3, pix, _w1,  h,  QRect(0,      _h1,    _w1,  h2));
00053     initPixmap(4, pix,  w,   h,  QRect(_w1,    _h1,     w2,  h2));
00054     initPixmap(5, pix, _w3,  h,  QRect(_w1+w2, _h1,    _w3,  h2));
00055     initPixmap(6, pix, _w1, _h3, QRect(0,      _h1+h2, _w1, _h3));
00056     initPixmap(7, pix,  w,  _h3, QRect(_w1,    _h1+h2,  w2, _h3));
00057     initPixmap(8, pix, _w3, _h3, QRect(_w1+w2, _h1+h2, _w3, _h3));
00058 }
00059 
00060 TileSet::TileSet(const QPixmap &pix, int w1, int h1, int w3, int h3, int x1, int y1, int w2, int h2)
00061     : _w1(w1), _h1(h1), _w3(w3), _h3(h3)
00062 {
00063     if (pix.isNull())
00064         return;
00065 
00066     int x2 = pix.width() - _w3;
00067     int y2 = pix.height() - _h3;
00068     int w = w2; while (w < 32 && w2 > 0) w += w2;
00069     int h = h2; while (h < 32 && h2 > 0) h += h2;
00070 
00071     initPixmap(0, pix, _w1, _h1, QRect(0,  0,  _w1, _h1));
00072     initPixmap(1, pix,  w,  _h1, QRect(x1, 0,   w2, _h1));
00073     initPixmap(2, pix, _w3, _h1, QRect(x2, 0,  _w3, _h1));
00074     initPixmap(3, pix, _w1,  h,  QRect(0,  y1, _w1,  h2));
00075     initPixmap(4, pix,  w,   h,  QRect(x1, y1,  w2,  h2));
00076     initPixmap(5, pix, _w3,  h,  QRect(x2, y1, _w3,  h2));
00077     initPixmap(6, pix, _w1, _h3, QRect(0,  y2, _w1, _h3));
00078     initPixmap(7, pix,  w,  _h3, QRect(x1, y2,  w2, _h3));
00079     initPixmap(8, pix, _w3, _h3, QRect(x2, y2, _w3, _h3));
00080 }
00081 
00082 TileSet::TileSet(const TileSet &other)
00083     : _w1(other._w1), _h1(other._h1), _w3(other._w3), _h3(other._h3)
00084 {
00085     for (int i=0; i<9; i++) {
00086         _pixmap[i] = other._pixmap[i];
00087     }
00088 }
00089 
00090 TileSet& TileSet::operator=(const TileSet &other)
00091 {
00092     _w1 = other._w1;
00093     _w3 = other._w3;
00094     _h1 = other._h1;
00095     _h3 = other._h3;
00096     for (int i=0; i<9; i++) {
00097         _pixmap[i] = other._pixmap[i];
00098     }
00099     return *this;
00100 }
00101 
00102 inline bool bits(TileSet::Tiles flags, TileSet::Tiles testFlags)
00103 {
00104     return (flags & testFlags) == testFlags;
00105 }
00106 
00107 void TileSet::render(const QRect &r, QPainter *p, Tiles t) const
00108 {
00109     if (_pixmap[0].isNull())
00110         return;
00111 
00112     int x0, y0, w, h;
00113     r.getRect(&x0, &y0, &w, &h);
00114     w -= _w1 + _w3;
00115     h -= _h1 + _h3;
00116     int x1 = x0 + _w1;
00117     int x2 = x1 + w;
00118     int y1 = y0 + _h1;
00119     int y2 = y1 + h;
00120 
00121     if (bits(t,    Top|Left))  p->drawPixmap(x0, y0, _pixmap[0]);
00122     if (bits(t,    Top|Right)) p->drawPixmap(x2, y0, _pixmap[2]);
00123     if (bits(t, Bottom|Left))  p->drawPixmap(x0, y2, _pixmap[6]);
00124     if (bits(t, Bottom|Right)) p->drawPixmap(x2, y2, _pixmap[8]);
00125 
00126     if (t & Top)    p->drawTiledPixmap(x1, y0, w, _h1, _pixmap[1]);
00127     if (t & Bottom) p->drawTiledPixmap(x1, y2, w, _h3, _pixmap[7]);
00128     if (t & Left)   p->drawTiledPixmap(x0, y1, _w1, h, _pixmap[3]);
00129     if (t & Right)  p->drawTiledPixmap(x2, y1, _w3, h, _pixmap[5]);
00130 
00131     if (t & Center) p->drawTiledPixmap(x1, y1, w, h, _pixmap[4]);
00132 }

KStyles

Skip menu "KStyles"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KCMShell
  • KNotify
  • KStyles
  • Nepomuk Daemons
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal