File: qguiplatformplugin_kde.cpp

package info (click to toggle)
kde-workspace 4:4.11.13-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 67,756 kB
  • ctags: 47,705
  • sloc: cpp: 358,638; ansic: 34,695; xml: 5,231; perl: 1,598; sh: 1,307; ruby: 1,135; python: 651; asm: 566; makefile: 37
file content (389 lines) | stat: -rw-r--r-- 12,379 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*  This file is part of the KDE project

    Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). <qt-info@nokia.com>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include <KDE/KStandardDirs>
#include <KDE/KGlobal>
#include <KDE/KComponentData>
#include <KDE/KGlobalSettings>
#include <KDE/KStyle>
#include <KDE/KConfigGroup>
#include <KDE/KIcon>
#include <KDE/KFileDialog>
#include <KDE/KColorDialog>
#include <QtCore/QHash>
#include <QtCore/QTimer>
#include <QtGui/QFileDialog>
#include <QtGui/QColorDialog>
#include <QtGui/QApplication>
#include <QtGui/QToolButton>
#include <QtGui/QToolBar>
#include <QtGui/QMainWindow>
#include "qguiplatformplugin_p.h"

#include <kdebug.h>

/*
 * Map a Qt filter string into a KDE one.
 * (from kfiledialog.cpp)
*/
static QString qt2KdeFilter(const QString &f)
{
    QString filter;
    QTextStream str(&filter, QIODevice::WriteOnly);
    const QStringList list(f.split(";;").replaceInStrings("/", "\\/"));
    QStringList::const_iterator it(list.begin()),
                                end(list.end());
    bool first=true;

    for(; it!=end; ++it)
    {
        int ob=(*it).lastIndexOf('('),
            cb=(*it).lastIndexOf(')');

        if(-1!=cb && ob<cb)
        {
            if(first)
                first=false;
            else
                str << '\n';
            str << (*it).mid(ob+1, (cb-ob)-1) << '|' << (*it).mid(0, ob);
        }
    }
    return filter;
}

/*
 * Map a KDE filter string into a Qt one.
 * (from kfiledialog.cpp)
 */
static void kde2QtFilter(const QString &orig, const QString &kde, QString *sel)
{
    if(sel)
    {
        const QStringList list(orig.split(";;"));
        QStringList::const_iterator it(list.begin()),
                                    end(list.end());
        int pos;

        for(; it!=end; ++it)
            if(-1!=(pos=(*it).indexOf(kde)) && pos>0 &&
                ('('==(*it)[pos-1] || ' '==(*it)[pos-1]) &&
                (*it).length()>=kde.length()+pos &&
                (')'==(*it)[pos+kde.length()] || ' '==(*it)[pos+kde.length()]))
            {
                *sel=*it;
                return;
            }
    }
}


class KFileDialogBridge : public KFileDialog
{
public:
    KFileDialogBridge (const KUrl &startDir, const QString &filter, QFileDialog *original_)
     :  KFileDialog (startDir, filter, original_), original(original_)
     {
         connect(this, SIGNAL(fileSelected(QString)), original, SIGNAL(currentChanged(QString)));
     }

    virtual void accept()
    {
        kDebug();
        KFileDialog::accept();
        QMetaObject::invokeMethod(original, "accept"); //workaround protected
    }

    virtual void reject()
    {
        kDebug();
        KFileDialog::reject();
        QMetaObject::invokeMethod(original, "reject"); //workaround protected
    }

    QFileDialog *original;
};

class KColorDialogBridge : public KColorDialog
{
public:
    KColorDialogBridge(QColorDialog* original_ = 0L) : KColorDialog(original_, true) , original(original_)
    {
        connect(this, SIGNAL(colorSelected(QColor)), original, SIGNAL(currentColorChanged(QColor)));
    }

    QColorDialog *original;

    virtual void accept()
    {
        KColorDialog::accept();
        original->setCurrentColor(color());
        QMetaObject::invokeMethod(original, "accept"); //workaround protected
    }

    virtual void reject()
    {
        KColorDialog::reject();
        QMetaObject::invokeMethod(original, "reject"); //workaround protected
    }
};

Q_DECLARE_METATYPE(KFileDialogBridge *)
Q_DECLARE_METATYPE(KColorDialogBridge *)

class KQGuiPlatformPlugin : public QGuiPlatformPlugin
{
    Q_OBJECT
public:
    KQGuiPlatformPlugin()
    {
        QMetaObject::invokeMethod(this, "init", Qt::QueuedConnection);
    }

    virtual QStringList keys() const { return QStringList() << QLatin1String("kde"); }
    virtual QString styleName()
    {
        const QString defaultStyle = KStyle::defaultStyle();
        const KConfigGroup pConfig(KGlobal::config(), "General");
        return pConfig.readEntry("widgetStyle", defaultStyle);
    }
    virtual QPalette palette()
    {
        return KGlobalSettings::createApplicationPalette();
    }
    virtual QString systemIconThemeName()
    {
        return KIconTheme::current();
    }
    virtual QStringList iconThemeSearchPaths()
    {
        return KGlobal::dirs()->resourceDirs("icon");
    }
    virtual QIcon fileSystemIcon(const QFileInfo &file)
    {
        return KIcon(KMimeType::findByPath(file.filePath(), 0, true)->iconName());
    }
    virtual int platformHint(PlatformHint hint)
    {
        switch(hint)
        {
        case PH_ToolButtonStyle: {
            KConfigGroup group(KGlobal::config(), "Toolbar style");
            QString style = group.readEntry("ToolButtonStyle", "TextUnderIcon").toLower();
            if (style == "textbesideicon" || style == "icontextright")
                return Qt::ToolButtonTextBesideIcon;
            else if (style == "textundericon" || style == "icontextbottom")
                return Qt::ToolButtonTextUnderIcon;
            else if (style == "textonly")
                return Qt::ToolButtonTextOnly;
            else
                return Qt::ToolButtonIconOnly;
        }
        case PH_ToolBarIconSize:
            return KIconLoader::global()->currentSize(KIconLoader::MainToolbar);
        case PH_ItemView_ActivateItemOnSingleClick:
            return KGlobalSettings::singleClick();
        default:
            break;
        }
        return QGuiPlatformPlugin::platformHint(hint);
    }

public: // File Dialog integration
#define K_FD(QFD) KFileDialogBridge *kdefd = qvariant_cast<KFileDialogBridge *>(QFD->property("_k_bridge"))
    virtual void fileDialogDelete(QFileDialog *qfd)
    {
        K_FD(qfd);
        delete kdefd;
    }
    virtual bool fileDialogSetVisible(QFileDialog *qfd, bool visible)
    {
        K_FD(qfd);
        if (!kdefd && visible) {
            if(qfd->options() & QFileDialog::DontUseNativeDialog)
                return false;

            kdefd = new KFileDialogBridge(KUrl::fromPath(qfd->directory().canonicalPath()),
                                          qt2KdeFilter(qfd->nameFilters().join(";;")), qfd);

            qfd->setProperty("_k_bridge", QVariant::fromValue(kdefd));
        }

        if (visible) {
            switch (qfd->fileMode()) {
                case QFileDialog::AnyFile:
                    kdefd->setMode(KFile::LocalOnly | KFile::File);
                    break;
                case QFileDialog::ExistingFile:
                    kdefd->setMode(KFile::LocalOnly | KFile::File | KFile::ExistingOnly);
                    break;
                case QFileDialog::ExistingFiles:
                    kdefd->setMode(KFile::LocalOnly | KFile::Files | KFile::ExistingOnly);
                    break;
                case QFileDialog::Directory:
                case QFileDialog::DirectoryOnly:
                    kdefd->setMode(KFile::LocalOnly | KFile::Directory);
                    break;
            }


            kdefd->setOperationMode((qfd->acceptMode() == QFileDialog::AcceptSave) ? KFileDialog::Saving : KFileDialog::Opening);
            kdefd->setCaption(qfd->windowTitle());
            kdefd->setConfirmOverwrite(qfd->confirmOverwrite());
            kdefd->setSelection(qfd->selectedFiles().value(0));
        }
        if (kdefd) {
            kdefd->setVisible(visible);
        }
        return true;
    }
    virtual QDialog::DialogCode fileDialogResultCode(QFileDialog *qfd)
    {
        K_FD(qfd);
        Q_ASSERT(kdefd);
        return QDialog::DialogCode(kdefd->result());
    }
    virtual void fileDialogSetDirectory(QFileDialog *qfd, const QString &directory)
    {
        K_FD(qfd);
        kdefd->setUrl(KUrl::fromPath(directory));
    }
    virtual QString fileDialogDirectory(const QFileDialog *qfd) const
    {
        K_FD(qfd);
        Q_ASSERT(kdefd);
        return kdefd->baseUrl().pathOrUrl();
    }
    virtual void fileDialogSelectFile(QFileDialog *qfd, const QString &filename)
    {
        K_FD(qfd);
        Q_ASSERT(kdefd);
        kdefd->setSelection(filename);
    }
    virtual QStringList fileDialogSelectedFiles(const QFileDialog *qfd) const
    {
        K_FD(qfd);
        Q_ASSERT(kdefd);
        return kdefd->selectedFiles();
    }
    /*virtual void fileDialogSetFilter(QFileDialog *qfd)
    {
        K_FD(qfd);
    }*/
    virtual void fileDialogSetNameFilters(QFileDialog *qfd, const QStringList &filters)
    {
        K_FD(qfd);
        Q_ASSERT(kdefd);
        kdefd->setFilter(qt2KdeFilter(filters.join(";;")));
    }
    /*virtual void fileDialogSelectNameFilter(QFileDialog *qfd, const QString &filter)
    {
        K_FD(qfd);
    }*/
    virtual QString fileDialogSelectedNameFilter(const QFileDialog *qfd) const
    {
        K_FD(qfd);
        Q_ASSERT(kdefd);
        QString ret;
        kde2QtFilter(qfd->nameFilters().join(";;"), kdefd->currentFilter(), &ret);
        return ret;
    }
public: // ColorDialog
#define K_CD(QCD) KColorDialogBridge *kdecd = qvariant_cast<KColorDialogBridge *>(QCD->property("_k_bridge"))
    virtual void colorDialogDelete(QColorDialog *qcd)
    {
        K_CD(qcd);
        delete kdecd;

    }
    virtual bool colorDialogSetVisible(QColorDialog *qcd, bool visible)
    {
        K_CD(qcd);
        if (!kdecd) {
            kdecd = new KColorDialogBridge(qcd);
            kdecd->setColor(qcd->currentColor());
            if (qcd->options() & QColorDialog::NoButtons) {
                kdecd->setButtons(KDialog::None);
            }
            kdecd->setModal(qcd->isModal());
            qcd->setProperty("_k_bridge", QVariant::fromValue(kdecd));
        }
        if (visible) {
            kdecd->setCaption(qcd->windowTitle());
            kdecd->setAlphaChannelEnabled(qcd->options() & QColorDialog::ShowAlphaChannel);
        }
        kdecd->setVisible(visible);
        return true;
    }
    virtual void colorDialogSetCurrentColor(QColorDialog *qcd, const QColor &color)
    {
        K_CD(qcd);
        if (kdecd) {
            kdecd->setColor(color);
        }
    }

private slots:
    void init()
    {
        connect(KIconLoader::global(), SIGNAL(iconLoaderSettingsChanged()), this, SLOT(updateToolbarIcons()));
        connect(KGlobalSettings::self(), SIGNAL(toolbarAppearanceChanged(int)), this, SLOT(updateToolbarStyle()));
        connect(KGlobalSettings::self(), SIGNAL(kdisplayStyleChanged()), this, SLOT(updateWidgetStyle()));
    }

    void updateToolbarStyle()
    {
        //from gtksymbol.cpp
        QWidgetList widgets = QApplication::allWidgets();
        for (int i = 0; i < widgets.size(); ++i) {
            QWidget *widget = widgets.at(i);
            if (qobject_cast<QToolButton*>(widget)) {
                QEvent event(QEvent::StyleChange);
                QApplication::sendEvent(widget, &event);
            }
        }
    }

    void updateToolbarIcons()
    {
        QWidgetList widgets = QApplication::allWidgets();
        for (int i = 0; i < widgets.size(); ++i) {
            QWidget *widget = widgets.at(i);
            if (qobject_cast<QToolBar*>(widget) || qobject_cast<QMainWindow*>(widget)) {
                QEvent event(QEvent::StyleChange);
                QApplication::sendEvent(widget, &event);
            }
        }
    }

    void updateWidgetStyle()
    {
        if (qApp) {
            if (qApp->style()->objectName() != styleName()) {
                qApp->setStyle(styleName());
            }
        }
    }
};

Q_EXPORT_PLUGIN2(KQGuiPlatformPlugin, KQGuiPlatformPlugin)

#include "qguiplatformplugin_kde.moc"