not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kcontrol / input / xcursor / cursortheme.cpp
blob92abea5376a606256a4e0e52774c7916172d0cfb
1 /*
2 * Copyright © 2006-2007 Fredrik Höglund <fredrik@kde.org>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License version 2 or at your option version 3 as published
7 * by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; see the file COPYING. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 #include <QApplication>
21 #include <QStyle>
22 #include <QCursor>
23 #include <QImage>
24 #include <QFile>
25 #include <QX11Info>
27 #include "themepage.h"
28 #include "cursortheme.h"
30 #include <config-X11.h>
32 #ifdef HAVE_XFIXES
33 # include <X11/Xlib.h>
34 # include <X11/extensions/Xfixes.h>
35 #endif
38 CursorTheme::CursorTheme(const QString &title, const QString &description)
40 setTitle(title);
41 setDescription(description);
42 setSample("left_ptr");
43 setIsHidden(false);
44 setIsWritable(false);
48 QPixmap CursorTheme::icon() const
50 if (m_icon.isNull())
51 m_icon = createIcon();
53 return m_icon;
57 QImage CursorTheme::autoCropImage(const QImage &image) const
59 // Compute an autocrop rectangle for the image
60 QRect r(image.rect().bottomRight(), image.rect().topLeft());
61 const quint32 *pixels = reinterpret_cast<const quint32*>(image.bits());
63 for (int y = 0; y < image.height(); y++)
65 for (int x = 0; x < image.width(); x++)
67 if (*(pixels++))
69 if (x < r.left()) r.setLeft(x);
70 if (x > r.right()) r.setRight(x);
71 if (y < r.top()) r.setTop(y);
72 if (y > r.bottom()) r.setBottom(y);
77 // Normalize the rectangle
78 return image.copy(r.normalized());
82 QPixmap CursorTheme::loadPixmap(const QString &name, int size) const
84 QImage image = loadImage(name, size);
85 if (image.isNull())
86 return QPixmap();
88 return QPixmap::fromImage(image);
92 static int nominalCursorSize(int iconSize)
94 for (int i = 512; i > 8; i /= 2)
96 if (i < iconSize)
97 return i;
99 if ((i * .75) < iconSize)
100 return int(i * .75);
103 return 8;
107 QPixmap CursorTheme::createIcon() const
109 int iconSize = QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize);
110 int cursorSize = nominalCursorSize(iconSize);
111 QSize size = QSize(iconSize, iconSize);
113 QPixmap pixmap;
114 QImage image = loadImage(sample(), cursorSize);
116 if (image.isNull() && sample() != "left_ptr")
117 image = loadImage("left_ptr", cursorSize);
119 if (!image.isNull())
121 // Scale the image if it's larger than the preferred icon size
122 if (image.width() > size.width() || image.height() > size.height())
123 image = image.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
125 pixmap = QPixmap::fromImage(image);
128 return pixmap;
131 void CursorTheme::setCursorName(QCursor &cursor, const QString &name) const
133 #ifdef HAVE_XFIXES
134 static bool haveXfixes = ThemePage::haveXfixes();
136 if (haveXfixes)
138 XFixesSetCursorName(QX11Info::display(), cursor.handle(),
139 QFile::encodeName(name));
141 #endif