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>
27 #include "themepage.h"
28 #include "cursortheme.h"
30 #include <config-X11.h>
33 # include <X11/Xlib.h>
34 # include <X11/extensions/Xfixes.h>
38 CursorTheme::CursorTheme(const QString
&title
, const QString
&description
)
41 setDescription(description
);
42 setSample("left_ptr");
48 QPixmap
CursorTheme::icon() const
51 m_icon
= createIcon();
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
++)
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
);
88 return QPixmap::fromImage(image
);
92 static int nominalCursorSize(int iconSize
)
94 for (int i
= 512; i
> 8; i
/= 2)
99 if ((i
* .75) < iconSize
)
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
);
114 QImage image
= loadImage(sample(), cursorSize
);
116 if (image
.isNull() && sample() != "left_ptr")
117 image
= loadImage("left_ptr", cursorSize
);
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
);
131 void CursorTheme::setCursorName(QCursor
&cursor
, const QString
&name
) const
134 static bool haveXfixes
= ThemePage::haveXfixes();
138 XFixesSetCursorName(QX11Info::display(), cursor
.handle(),
139 QFile::encodeName(name
));