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

« back to all changes in this revision

Viewing changes to plasma/desktop/shell/activitymanager/filterbar.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) 2009 by Ana Cecília Martins <anaceciliamb@gmail.com>
 
3
 *   Copyright 2010 Chani Armitage <chani@kde.org>
 
4
 *
 
5
 *   This program is free software; you can redistribute it and/or modify
 
6
 *   it under the terms of the GNU Library/Lesser General Public License
 
7
 *   version 2, or (at your option) any later version, as published by the
 
8
 *   Free Software Foundation
 
9
 *
 
10
 *   This program is distributed in the hope that it will be useful,
 
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *   GNU General Public License for more details
 
14
 *
 
15
 *   You should have received a copy of the GNU Library/Lesser General Public
 
16
 *   License along with this program; if not, write to the
 
17
 *   Free Software Foundation, Inc.,
 
18
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
19
 */
 
20
 
 
21
#include "filterbar.h"
 
22
#include "desktopcorona.h"
 
23
#include "plasmaapp.h"
 
24
 
 
25
#include <QGraphicsLinearLayout>
 
26
#include <QTimer>
 
27
 
 
28
#include <klineedit.h>
 
29
#include <kmenu.h>
 
30
#include <kpushbutton.h>
 
31
#include <KServiceTypeTrader>
 
32
#include <KStandardDirs>
 
33
#include <knewstuff3/downloaddialog.h>
 
34
 
 
35
#include <plasma/theme.h>
 
36
#include <plasma/corona.h>
 
37
#include <plasma/widgets/lineedit.h>
 
38
#include <plasma/widgets/pushbutton.h>
 
39
#include <Plasma/TabBar>
 
40
#include <Plasma/Package>
 
41
 
 
42
#include <scripting/layouttemplatepackagestructure.h>
 
43
#include "scripting/desktopscriptengine.h"
 
44
 
 
45
FilterBar::FilterBar(Qt::Orientation orientation, QGraphicsItem *parent)
 
46
    : QGraphicsWidget(parent),
 
47
      m_unlockButton(0)
 
48
{
 
49
    setFocusPolicy(Qt::StrongFocus);
 
50
 
 
51
    //init text search
 
52
    m_textSearch = new Plasma::LineEdit();
 
53
 
 
54
    m_textSearch->nativeWidget()->setClickMessage(i18n("Enter Search Term"));
 
55
    m_textSearch->setAttribute(Qt::WA_NoSystemBackground);
 
56
    m_textSearch->setClearButtonShown(true);
 
57
    connect(m_textSearch, SIGNAL(textChanged(QString)), this, SIGNAL(searchTermChanged(QString)));
 
58
 
 
59
    /*
 
60
    m_categoriesTabs = new Plasma::TabBar(this);
 
61
    connect(m_categoriesTabs, SIGNAL(currentChanged(int)), this, SIGNAL(filterChanged(int)));
 
62
    m_categoriesTabs->setAttribute(Qt::WA_NoSystemBackground);
 
63
    m_categoriesTabs->nativeWidget()->setUsesScrollButtons(true);
 
64
    m_categoriesTabs->addTab(i18n("Running"));
 
65
    m_categoriesTabs->addTab(i18n("Stopped"));
 
66
    */
 
67
 
 
68
    m_addWidgetsButton = new Plasma::PushButton(this);
 
69
    m_addWidgetsButton->setText(i18n("Add Widgets"));
 
70
    m_addWidgetsButton->setIcon(KIcon("plasma"));
 
71
    connect(m_addWidgetsButton, SIGNAL(clicked()), this, SIGNAL(addWidgetsRequested()));
 
72
 
 
73
    m_newActivityButton = new Plasma::PushButton(this);
 
74
    m_newActivityButton->setText(i18n("Create Activity"));
 
75
    m_newActivityButton->setIcon(KIcon("list-add"));
 
76
    m_newActivityMenu = new KMenu();
 
77
    connect(m_newActivityMenu, SIGNAL(aboutToShow()), this, SLOT(populateActivityMenu()));
 
78
    connect(m_newActivityMenu, SIGNAL(triggered(QAction*)), this, SLOT(createActivity(QAction*)));
 
79
    m_newActivityButton->nativeWidget()->setMenu(m_newActivityMenu);
 
80
    //m_newWidgetsButton->setMinimumWidth(m_newWidgetsButton->effectiveSizeHint(Qt::PreferredSize).width());
 
81
 
 
82
    //layout
 
83
    m_linearLayout = new QGraphicsLinearLayout(this);
 
84
    m_linearLayout->addItem(m_textSearch);
 
85
    //m_linearLayout->addItem(m_categoriesTabs);
 
86
    m_linearLayout->addStretch(10);
 
87
    m_linearLayout->addItem(m_newActivityButton);
 
88
    m_linearLayout->addItem(m_addWidgetsButton);
 
89
 
 
90
    QTimer::singleShot(0, this, SLOT(registerToCoronaChanges()));
 
91
    setOrientation(orientation);
 
92
}
 
93
 
 
94
FilterBar::~FilterBar()
 
95
{
 
96
}
 
97
 
 
98
Plasma::LineEdit* FilterBar::textSearch()
 
99
{
 
100
    return m_textSearch;
 
101
}
 
102
 
 
103
void FilterBar::resizeEvent(QGraphicsSceneResizeEvent *event)
 
104
{
 
105
    Q_UNUSED(event)
 
106
 
 
107
    QSizeF contentsSize = m_linearLayout->contentsRect().size();
 
108
//    m_linearLayout->invalidate();
 
109
//    m_linearLayout->activate();
 
110
 
 
111
    if(m_orientation == Qt::Horizontal) {
 
112
        //don't let it occupy the whole layout width
 
113
        m_textSearch->setMaximumWidth(contentsSize.width()/6);
 
114
        m_textSearch->setMinimumWidth(contentsSize.width()/6);
 
115
    } else {
 
116
        //let it occupy the whole width
 
117
        m_textSearch->setMaximumWidth(-1);
 
118
        m_textSearch->setMinimumWidth(-1);
 
119
    }
 
120
}
 
121
 
 
122
void FilterBar::setOrientation(Qt::Orientation orientation)
 
123
{
 
124
    m_orientation = orientation;
 
125
    m_linearLayout->setOrientation(orientation);
 
126
 
 
127
    if (orientation == Qt::Horizontal) {
 
128
        m_textSearch->setPreferredWidth(200);
 
129
        m_textSearch->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
 
130
        m_textSearch->setPreferredHeight(-1);
 
131
    } else {
 
132
        m_textSearch->setPreferredHeight(30);
 
133
        m_textSearch->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
 
134
        m_textSearch->setPreferredWidth(-1);
 
135
    }
 
136
}
 
137
 
 
138
void FilterBar::setMenuPos()
 
139
{
 
140
    QPoint position(0, 0);
 
141
    Plasma::Corona *corona = qobject_cast<Plasma::Corona*>(scene());
 
142
    if (corona) {
 
143
        position = corona->popupPosition(m_newActivityButton,
 
144
                m_newActivityMenu->geometry().size());
 
145
    }
 
146
    m_newActivityMenu->move(position);
 
147
}
 
148
 
 
149
void FilterBar::setFocus()
 
150
{
 
151
    m_textSearch->setFlag(ItemIsFocusable);
 
152
    m_textSearch->setFocus();
 
153
}
 
154
 
 
155
void FilterBar::populateActivityMenu()
 
156
{
 
157
    QTimer::singleShot(0, this, SLOT(setMenuPos()));
 
158
    if (!m_newActivityMenu->actions().isEmpty()) {
 
159
        // already populated.
 
160
        return;
 
161
    }
 
162
 
 
163
    QMenu *templatesMenu = m_newActivityMenu->addMenu(i18n("Templates"));
 
164
    QMap<QString, QAction*> sorted; //qmap sorts alphabetically
 
165
 
 
166
    //regular plugins
 
167
    KPluginInfo::List plugins = Plasma::Containment::listContainmentsOfType("desktop");
 
168
    foreach (const KPluginInfo& info, plugins) {
 
169
        if (info.property("NoDisplay").toBool()) {
 
170
            continue;
 
171
        }
 
172
        QAction *action;
 
173
        if (info.pluginName() == "desktop") { //suggest this one for newbies
 
174
            action = m_newActivityMenu->addAction(KIcon(info.icon()), i18n("Empty Desktop"));
 
175
        } else {
 
176
            action = new QAction(KIcon(info.icon()), info.name(), templatesMenu);
 
177
            sorted.insert(info.name(), action);
 
178
        }
 
179
        action->setData(info.pluginName());
 
180
    }
 
181
 
 
182
    //templates
 
183
    const QString constraint = QString("[X-Plasma-Shell] == '%1' and 'desktop' ~in [X-Plasma-ContainmentCategories]")
 
184
                                      .arg(KGlobal::mainComponent().componentName());
 
185
    KService::List templates = KServiceTypeTrader::self()->query("Plasma/LayoutTemplate", constraint);
 
186
    foreach (const KService::Ptr &service, templates) {
 
187
        KPluginInfo info(service);
 
188
        Plasma::PackageStructure::Ptr structure(new WorkspaceScripting::LayoutTemplatePackageStructure);
 
189
        const QString path = KStandardDirs::locate("data", structure->defaultPackageRoot() + '/' + info.pluginName() + '/');
 
190
        if (!path.isEmpty()) {
 
191
            Plasma::Package package(path, structure);
 
192
            const QString scriptFile = package.filePath("mainscript");
 
193
            const QStringList startupApps = service->property("X-Plasma-ContainmentLayout-ExecuteOnCreation", QVariant::StringList).toStringList();
 
194
 
 
195
            if (!scriptFile.isEmpty() || !startupApps.isEmpty()) {
 
196
                QAction *action = new QAction(KIcon(info.icon()), info.name(), templatesMenu);
 
197
                QVariantList data;
 
198
                data << scriptFile << info.name() << info.icon() << startupApps;
 
199
                action->setData(data);
 
200
                sorted.insert(info.name(), action);
 
201
            }
 
202
        }
 
203
    }
 
204
 
 
205
    //set up sorted menu
 
206
    foreach (QAction *action, sorted) {
 
207
        templatesMenu->addAction(action);
 
208
    }
 
209
 
 
210
    //clone
 
211
    QAction *action = m_newActivityMenu->addAction(KIcon("edit-copy"), i18n("Clone current activity"));
 
212
    action->setData(0);
 
213
 
 
214
    //ghns
 
215
    templatesMenu->addSeparator();
 
216
    action = templatesMenu->addAction(KIcon("get-hot-new-stuff"), i18n("Get New Templates..."));
 
217
    action->setData(1);
 
218
}
 
219
 
 
220
void FilterBar::createActivity(QAction *action)
 
221
{
 
222
    QVariant::Type type = action->data().type();
 
223
    if (type == QVariant::String) {
 
224
        QString plugin = action->data().toString();
 
225
        PlasmaApp::self()->createActivity(plugin);
 
226
    } else if (type == QVariant::List) {
 
227
        QVariantList data = action->data().toList();
 
228
        PlasmaApp::self()->createActivityFromScript(data[0].toString(),
 
229
                                                    data[1].toString(),
 
230
                                                    data[2].toString(),
 
231
                                                    data[3].toStringList());
 
232
    } else if (action->data().toInt() == 0) {
 
233
        PlasmaApp::self()->cloneCurrentActivity();
 
234
    } else { //ghns
 
235
        KNS3::DownloadDialog *dialog = new KNS3::DownloadDialog( "activities.knsrc", 0 );
 
236
        connect(dialog, SIGNAL(accepted()), m_newActivityMenu, SLOT(clear()));
 
237
        connect(dialog, SIGNAL(accepted()), dialog, SLOT(deleteLater()));
 
238
        dialog->show();
 
239
    }
 
240
}
 
241
 
 
242
void FilterBar::registerToCoronaChanges()
 
243
{
 
244
    Plasma::Corona *corona = qobject_cast<Plasma::Corona*>(scene());
 
245
    if (corona) {
 
246
        connect(corona, SIGNAL(immutabilityChanged(Plasma::ImmutabilityType)), this, SLOT(coronaImmutabilityChanged(Plasma::ImmutabilityType)));
 
247
        coronaImmutabilityChanged(corona->immutability());
 
248
    }
 
249
}
 
250
 
 
251
void FilterBar::coronaImmutabilityChanged(Plasma::ImmutabilityType immutability)
 
252
{
 
253
    m_newActivityButton->setVisible(immutability == Plasma::Mutable);
 
254
    m_addWidgetsButton->setVisible(immutability == Plasma::Mutable);
 
255
    if (immutability == Plasma::UserImmutable) {
 
256
        Plasma::Corona *corona = qobject_cast<Plasma::Corona*>(scene());
 
257
        if (corona) {
 
258
            m_unlockButton = new Plasma::PushButton(this);
 
259
            m_unlockButton->setAction(corona->action("lock widgets"));
 
260
            m_linearLayout->addItem(m_unlockButton);
 
261
        }
 
262
    } else if (m_unlockButton) {
 
263
        m_unlockButton->deleteLater();
 
264
        m_unlockButton = 0;
 
265
    }
 
266
}
 
267
 
 
268
#include "filterbar.moc"