2
* Copyright (C) 2009 by Ana Cecília Martins <anaceciliamb@gmail.com>
3
* Copyright 2010 Chani Armitage <chani@kde.org>
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
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
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.
21
#include "filterbar.h"
22
#include "desktopcorona.h"
23
#include "plasmaapp.h"
25
#include <QGraphicsLinearLayout>
28
#include <klineedit.h>
30
#include <kpushbutton.h>
31
#include <KServiceTypeTrader>
32
#include <KStandardDirs>
33
#include <knewstuff3/downloaddialog.h>
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>
42
#include <scripting/layouttemplatepackagestructure.h>
43
#include "scripting/desktopscriptengine.h"
45
FilterBar::FilterBar(Qt::Orientation orientation, QGraphicsItem *parent)
46
: QGraphicsWidget(parent),
49
setFocusPolicy(Qt::StrongFocus);
52
m_textSearch = new Plasma::LineEdit();
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)));
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"));
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()));
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());
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);
90
QTimer::singleShot(0, this, SLOT(registerToCoronaChanges()));
91
setOrientation(orientation);
94
FilterBar::~FilterBar()
98
Plasma::LineEdit* FilterBar::textSearch()
103
void FilterBar::resizeEvent(QGraphicsSceneResizeEvent *event)
107
QSizeF contentsSize = m_linearLayout->contentsRect().size();
108
// m_linearLayout->invalidate();
109
// m_linearLayout->activate();
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);
116
//let it occupy the whole width
117
m_textSearch->setMaximumWidth(-1);
118
m_textSearch->setMinimumWidth(-1);
122
void FilterBar::setOrientation(Qt::Orientation orientation)
124
m_orientation = orientation;
125
m_linearLayout->setOrientation(orientation);
127
if (orientation == Qt::Horizontal) {
128
m_textSearch->setPreferredWidth(200);
129
m_textSearch->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
130
m_textSearch->setPreferredHeight(-1);
132
m_textSearch->setPreferredHeight(30);
133
m_textSearch->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
134
m_textSearch->setPreferredWidth(-1);
138
void FilterBar::setMenuPos()
140
QPoint position(0, 0);
141
Plasma::Corona *corona = qobject_cast<Plasma::Corona*>(scene());
143
position = corona->popupPosition(m_newActivityButton,
144
m_newActivityMenu->geometry().size());
146
m_newActivityMenu->move(position);
149
void FilterBar::setFocus()
151
m_textSearch->setFlag(ItemIsFocusable);
152
m_textSearch->setFocus();
155
void FilterBar::populateActivityMenu()
157
QTimer::singleShot(0, this, SLOT(setMenuPos()));
158
if (!m_newActivityMenu->actions().isEmpty()) {
159
// already populated.
163
QMenu *templatesMenu = m_newActivityMenu->addMenu(i18n("Templates"));
164
QMap<QString, QAction*> sorted; //qmap sorts alphabetically
167
KPluginInfo::List plugins = Plasma::Containment::listContainmentsOfType("desktop");
168
foreach (const KPluginInfo& info, plugins) {
169
if (info.property("NoDisplay").toBool()) {
173
if (info.pluginName() == "desktop") { //suggest this one for newbies
174
action = m_newActivityMenu->addAction(KIcon(info.icon()), i18n("Empty Desktop"));
176
action = new QAction(KIcon(info.icon()), info.name(), templatesMenu);
177
sorted.insert(info.name(), action);
179
action->setData(info.pluginName());
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();
195
if (!scriptFile.isEmpty() || !startupApps.isEmpty()) {
196
QAction *action = new QAction(KIcon(info.icon()), info.name(), templatesMenu);
198
data << scriptFile << info.name() << info.icon() << startupApps;
199
action->setData(data);
200
sorted.insert(info.name(), action);
206
foreach (QAction *action, sorted) {
207
templatesMenu->addAction(action);
211
QAction *action = m_newActivityMenu->addAction(KIcon("edit-copy"), i18n("Clone current activity"));
215
templatesMenu->addSeparator();
216
action = templatesMenu->addAction(KIcon("get-hot-new-stuff"), i18n("Get New Templates..."));
220
void FilterBar::createActivity(QAction *action)
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(),
231
data[3].toStringList());
232
} else if (action->data().toInt() == 0) {
233
PlasmaApp::self()->cloneCurrentActivity();
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()));
242
void FilterBar::registerToCoronaChanges()
244
Plasma::Corona *corona = qobject_cast<Plasma::Corona*>(scene());
246
connect(corona, SIGNAL(immutabilityChanged(Plasma::ImmutabilityType)), this, SLOT(coronaImmutabilityChanged(Plasma::ImmutabilityType)));
247
coronaImmutabilityChanged(corona->immutability());
251
void FilterBar::coronaImmutabilityChanged(Plasma::ImmutabilityType immutability)
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());
258
m_unlockButton = new Plasma::PushButton(this);
259
m_unlockButton->setAction(corona->action("lock widgets"));
260
m_linearLayout->addItem(m_unlockButton);
262
} else if (m_unlockButton) {
263
m_unlockButton->deleteLater();
268
#include "filterbar.moc"