~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kcontrol/input/core/themepage.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2003 Fredrik Höglund <fredrik@kde.org>
 
3
 *
 
4
 * Based on the large cursor code written by Rik Hemsley,
 
5
 * Copyright (c) 2000 Rik Hemsley <rik@kde.org>
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU General Public
 
9
 * License version 2 as published by the Free Software Foundation.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 * General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; see the file COPYING.  If not, write to
 
18
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
19
 * Boston, MA 02110-1301, USA.
 
20
 */
 
21
 
 
22
#include <kglobal.h>
 
23
#include <kstandarddirs.h>
 
24
#include <kurl.h>
 
25
#include <kprocess.h>
 
26
#include <kio/job.h>
 
27
#include <kio/netaccess.h>
 
28
#include <kmessagebox.h>
 
29
#include <klocale.h>
 
30
#include <kconfig.h>
 
31
#include <kdialog.h>
 
32
 
 
33
#include <QBoxLayout>
 
34
#include <QDir>
 
35
#include <QLabel>
 
36
#include <QPixmap>
 
37
 
 
38
#include "themepage.h"
 
39
#include <QTreeWidget>
 
40
#include "themepage.moc"
 
41
 
 
42
#include "bitmaps.h"
 
43
 
 
44
 
 
45
 
 
46
ThemePage::ThemePage( QWidget* parent, const char* name )
 
47
        : QWidget( parent )
 
48
{
 
49
        setObjectName(name);
 
50
        QBoxLayout *layout = new QVBoxLayout( this );
 
51
        layout->setMargin( KDialog::marginHint() );
 
52
        layout->setSpacing( KDialog::spacingHint() );
 
53
 
 
54
        layout->addWidget(new QLabel( i18n("Select the cursor theme you want to use:"), this ));
 
55
 
 
56
        // Create the theme list view
 
57
        listview = new QTreeWidget( this );
 
58
        QStringList lstHeader;
 
59
        lstHeader<<i18n("Name")<<i18n("Description");
 
60
        listview->setHeaderLabels( lstHeader );
 
61
        listview->setSelectionMode( QAbstractItemView::SingleSelection );
 
62
        listview->setRootIsDecorated( false );
 
63
        connect( listview, SIGNAL(currentItemChanged ( QTreeWidgetItem *, QTreeWidgetItem *) )
 
64
                 , this, SLOT( selectionChanged( QTreeWidgetItem*, QTreeWidgetItem* ) ) );
 
65
        layout->addWidget( listview );
 
66
        insertThemes();
 
67
}
 
68
 
 
69
 
 
70
ThemePage::~ThemePage()
 
71
{
 
72
}
 
73
 
 
74
 
 
75
void ThemePage::selectionChanged( QTreeWidgetItem*current,QTreeWidgetItem*previous )
 
76
{
 
77
    Q_UNUSED( previous );
 
78
    selectedTheme = current->data( 0, Qt::UserRole + 1 ).toString();
 
79
    emit changed( selectedTheme != currentTheme );
 
80
}
 
81
 
 
82
 
 
83
void ThemePage::save()
 
84
{
 
85
        if ( currentTheme == selectedTheme )
 
86
                return;
 
87
 
 
88
        bool whiteCursor = selectedTheme.right( 5 ) == "White";
 
89
        bool largeCursor = selectedTheme.left( 5 ) == "Large";
 
90
 
 
91
        KConfig config( "kcminputrc" );
 
92
        KConfigGroup c( &config, "Mouse" );
 
93
        c.writeEntry( "LargeCursor", largeCursor );
 
94
        c.writeEntry( "WhiteCursor", whiteCursor );
 
95
 
 
96
        currentTheme = selectedTheme;
 
97
 
 
98
        fixCursorFile();
 
99
 
 
100
        KMessageBox::information( this, i18n("You have to restart KDE for these "
 
101
                                "changes to take effect."), i18n("Cursor Settings Changed"),
 
102
                                "CursorSettingsChanged" );
 
103
}
 
104
 
 
105
 
 
106
void ThemePage::load()
 
107
{
 
108
        bool largeCursor, whiteCursor;
 
109
 
 
110
        KConfig config( "kcminputrc" );
 
111
        KConfigGroup c( &config, "Mouse" );
 
112
        largeCursor = c.readEntry( "LargeCursor", false);
 
113
        whiteCursor = c.readEntry( "WhiteCursor", false);
 
114
 
 
115
        if ( largeCursor )
 
116
                currentTheme = whiteCursor ? "LargeWhite" : "LargeBlack";
 
117
        else
 
118
                currentTheme = whiteCursor ? "SmallWhite" : "SmallBlack";
 
119
 
 
120
        selectedTheme = currentTheme;
 
121
        for ( int i = 0;i <listview->topLevelItemCount();++i )
 
122
        {
 
123
            QTreeWidgetItem *item = listview->topLevelItem( i );
 
124
            if ( item && item->data(0, Qt::UserRole + 1 ) == currentTheme )
 
125
            {
 
126
                listview->setCurrentItem( item );
 
127
            }
 
128
        }
 
129
}
 
130
 
 
131
 
 
132
void ThemePage::defaults()
 
133
{
 
134
        currentTheme = selectedTheme = "SmallBlack";
 
135
        for ( int i = 0;i <listview->topLevelItemCount();++i )
 
136
        {
 
137
            QTreeWidgetItem *item = listview->topLevelItem( i );
 
138
            if ( item && item->data(0, Qt::UserRole + 1 ) == currentTheme )
 
139
            {
 
140
                listview->setCurrentItem( item );
 
141
            }
 
142
        }
 
143
}
 
144
 
 
145
 
 
146
void ThemePage::insertThemes()
 
147
{
 
148
    QList<QTreeWidgetItem *> lstChildren;
 
149
    QTreeWidgetItem *item = new QTreeWidgetItem(listview );
 
150
 
 
151
    item->setData( 0, Qt::DisplayRole,i18n("Small black")  );
 
152
    item->setData( 1, Qt::DisplayRole, i18n("Small black cursors") );
 
153
    item->setData( 0, Qt::DecorationRole, QPixmap( arrow_small_black_xpm ) );
 
154
    item->setData( 0, Qt::UserRole + 1,"SmallBlack" );
 
155
    lstChildren<<item;
 
156
 
 
157
    item = new QTreeWidgetItem(listview );
 
158
    item->setData( 0, Qt::DisplayRole, i18n("Large black") );
 
159
    item->setData( 1, Qt::DisplayRole, i18n("Large black cursors") );
 
160
    item->setData( 0, Qt::DecorationRole, QPixmap( arrow_large_black_xpm ) );
 
161
    item->setData( 0, Qt::UserRole + 1,"LargeBlack"  );
 
162
    lstChildren<<item;
 
163
 
 
164
    item = new QTreeWidgetItem(listview );
 
165
    item->setData( 0, Qt::DisplayRole, i18n("Small white") );
 
166
    item->setData( 1, Qt::DisplayRole, i18n("Small white cursors") );
 
167
    item->setData( 0, Qt::DecorationRole, QPixmap(arrow_small_white_xpm  ) );
 
168
    item->setData( 0, Qt::UserRole + 1,"SmallWhite" );
 
169
    lstChildren<<item;
 
170
 
 
171
    item = new QTreeWidgetItem(listview );
 
172
    item->setData( 0, Qt::DisplayRole, i18n("Large white") );
 
173
    item->setData( 1, Qt::DisplayRole, i18n("Large white cursors") );
 
174
    item->setData( 0, Qt::DecorationRole, QPixmap( arrow_large_white_xpm ) );
 
175
    item->setData( 0, Qt::UserRole + 1, "LargeWhite" );
 
176
    lstChildren<<item;
 
177
 
 
178
    listview->addTopLevelItems( lstChildren );
 
179
}
 
180
 
 
181
 
 
182
void ThemePage::fixCursorFile()
 
183
{
 
184
        // Make sure we have the 'font' resource dir registered and can find the
 
185
        // override dir.
 
186
        //
 
187
        // Next, if the user wants large cursors, copy the font
 
188
        // cursor_large.pcf.gz to (localkdedir)/share/fonts/override/cursor.pcf.gz.
 
189
        // Else remove the font cursor.pcf.gz from (localkdedir)/share/fonts/override.
 
190
        //
 
191
        // Run mkfontdir to update fonts.dir in that dir.
 
192
 
 
193
        KGlobal::dirs()->addResourceType( "font", 0, "share/fonts/" );
 
194
        KIO::mkdir( QDir::homePath() + "/.fonts/kde-override" );
 
195
        QString overrideDir = QDir::homePath() + "/.fonts/kde-override/";
 
196
 
 
197
        KUrl installedFont;
 
198
        installedFont.setPath( overrideDir + "cursor.pcf.gz" );
 
199
 
 
200
        if ( currentTheme == "SmallBlack" )
 
201
                KIO::NetAccess::del( installedFont, this );
 
202
        else {
 
203
                KUrl source;
 
204
 
 
205
                if ( currentTheme == "LargeBlack" )
 
206
                        source.setPath( KStandardDirs::locate("data", "kcminput/cursor_large_black.pcf.gz") );
 
207
                else if ( currentTheme == "LargeWhite" )
 
208
                        source.setPath( KStandardDirs::locate("data", "kcminput/cursor_large_white.pcf.gz") );
 
209
                else if ( currentTheme == "SmallWhite" )
 
210
                        source.setPath( KStandardDirs::locate("data", "kcminput/cursor_small_white.pcf.gz") );
 
211
 
 
212
                KIO::Job* job = KIO::file_copy( source, installedFont, -1, KIO::Overwrite );
 
213
                job->exec();
 
214
        }
 
215
 
 
216
        QString cmd = KGlobal::dirs()->findExe( "mkfontdir" );
 
217
        if ( !cmd.isEmpty() )
 
218
        {
 
219
                KProcess p;
 
220
                p << cmd << overrideDir;
 
221
                p.execute();
 
222
        }
 
223
}
 
224
 
 
225
// vim: set noet ts=4 sw=4: