• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KNewStuff

  • knewstuff
  • knewstuff2
  • ui
knewstuff2/ui/itemsview.cpp
Go to the documentation of this file.
1/*
2 This file is part of KNewStuff2.
3 Copyright (C) 2005 by Enrico Ros <eros.kde@email.it>
4 Copyright (C) 2005 - 2007 Josef Spillner <spillner@kde.org>
5 Copyright (C) 2007 Dirk Mueller <mueller@kde.org>
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21// own include
22#include "itemsview.h"
23
24// qt/kde includes
25#include <QtCore/QFile>
26#include <QtGui/QWidget>
27#include <QtCore/QTimer>
28#include <QtGui/QLayout>
29#include <QtGui/QImage>
30#include <QtGui/QFont>
31#include <QtGui/QComboBox>
32#include <QtGui/QPushButton>
33#include <QtCore/QMutableVectorIterator>
34#include <QtCore/QRect>
35#include <QtGui/QPainter>
36#include <QtGui/QScrollArea>
37#include <QtGui/QApplication>
38#include <QtGui/QTextDocument>
39#include <kaboutdata.h>
40#include <kapplication.h>
41#include <kcomponentdata.h>
42#include <kglobalsettings.h>
43#include <klocale.h>
44#include <klineedit.h>
45#include <kconfig.h>
46#include <kstandarddirs.h>
47#include <kmessagebox.h>
48#include <kdebug.h>
49#include <kiconloader.h>
50#include <kio/job.h>
51#include <kio/netaccess.h>
52#include <ktitlewidget.h>
53#include <ktoolinvocation.h>
54
55#include "knewstuff2/core/provider.h"
56#include "knewstuff2/core/providerhandler.h"
57#include "knewstuff2/core/entry.h"
58#include "knewstuff2/core/entryhandler.h"
59#include "knewstuff2/core/category.h"
60
61#include "knewstuff2/dxs/dxs.h"
62
63#include "knewstuff2/ui/qprogressindicator.h"
64
65// local includes
66#include "ui_DownloadDialog.h"
67#include "kdxsbutton.h"
68#include "qasyncimage_p.h"
69
70using namespace KNS;
71
72static bool NameSorter(const Entry* e1, const Entry* e2)
73{
74 return e1->name().representation() < e2->name().representation();
75}
76
77static bool RatingSorter(const Entry* e1, const Entry* e2)
78{
79 return e1->rating() < e2->rating();
80}
81
82static bool RecentSorter(const Entry* e1, const Entry* e2)
83{
84 // return > instead of < to sort in reverse order
85 return e1->releaseDate() > e2->releaseDate();
86}
87
88static bool DownloadsSorter(const Entry* e1, const Entry* e2)
89{
90 // return > instead of < to sort most downloads at the top
91 return e1->downloads() > e2->downloads();
92}
93
94ItemsView::ItemsView(QWidget* _parent)
95 : QListView(_parent),
96 m_currentProvider(0), m_currentFeed(0), m_root(0), m_sorting(0), m_engine(0)
97{
98 m_root = new QWidget(this);
99 setFrameStyle(QFrame::Plain | QFrame::StyledPanel);
100 setVerticalScrollMode(ScrollPerPixel);
101 //setWidgetResizable(true);
102}
103
104ItemsView::~ItemsView()
105{
106}
107
108void ItemsView::setEngine(DxsEngine *engine)
109{
110 m_engine = engine;
111}
112
113void ItemsView::setProvider(const Provider * provider, const Feed * feed)
114{
115 m_currentProvider = provider;
116 m_currentFeed = feed;
117 buildContents();
118}
119
120void ItemsView::setSorting(int sortType)
121{
122 m_sorting = sortType;
123 buildContents();
124}
125
126void ItemsView::setFeed(const Feed * feed)
127{
128 m_currentFeed = feed;
129 buildContents();
130}
131
132void ItemsView::setSearchText(const QString & text)
133{
134 m_searchText = text;
135 buildContents();
136}
137
138void ItemsView::updateItem(Entry *entry)
139{
140 // FIXME: change this to call updateEntry once it is complete
141// if (m_views.contains(entry)) {
142// m_views[entry]->setEntry(entry);
143 // }
144}
145
146void ItemsView::buildContents()
147{
148 m_views.clear();
149
150 m_root->setBackgroundRole(QPalette::Base);
151 QVBoxLayout* _layout = new QVBoxLayout(m_root);
152
153 if (m_currentFeed != NULL) {
154 Entry::List entries = m_currentFeed->entries();
155 //switch (m_sorting)
156 //{
157 // case 0:
158 // qSort(entries.begin(), entries.end(), NameSorter);
159 // break;
160 // case 1:
161 // qSort(entries.begin(), entries.end(), RatingSorter);
162 // break;
163 // case 2:
164 // qSort(entries.begin(), entries.end(), RecentSorter);
165 // break;
166 // case 3:
167 // qSort(entries.begin(), entries.end(), DownloadsSorter);
168 // break;
169 //}
170
171 Entry::List::iterator it = entries.begin(), iEnd = entries.end();
172 for (unsigned row = 0; it != iEnd; ++it) {
173 Entry* entry = (*it);
174
175 if (entry->name().representation().toLower().contains(m_searchText.toLower())) {
176 QHBoxLayout * itemLayout = new QHBoxLayout;
177 _layout->addLayout(itemLayout);
178
179 EntryView *part = new EntryView(m_root);
180 part->setBackgroundRole(row & 1 ? QPalette::AlternateBase : QPalette::Base);
181 itemLayout->addWidget(part);
182
183 QVBoxLayout * previewLayout = new QVBoxLayout;
184 itemLayout->insertLayout(0, previewLayout);
185
186 KDXSButton *dxsbutton = new KDXSButton(m_root);
187 dxsbutton->setEntry(entry);
188 dxsbutton->setProvider(m_currentProvider);
189 dxsbutton->setEngine(m_engine);
190
191 QString imageurl = entry->preview().representation();
192 if (!imageurl.isEmpty()) {
193 QLabel *f = new QLabel(m_root);
194 f->setFrameStyle(QFrame::Panel | QFrame::Sunken);
195 QAsyncImage *pix = new QAsyncImage(imageurl, m_root);
196 f->setFixedSize(64, 64);
197 //connect(pix, SIGNAL(signalLoaded(QImage)),
198 // f, SLOT(setImage(QImage)));
199 previewLayout->addWidget(f);
200 }
201 //previewLayout->addWidget(dxsbutton);
202
203 part->setEntry(entry);
204 m_views.insert(entry, part);
205 ++row;
206 }
207 }
208 }
209
210 //setWidget(m_root);
211}
212
213EntryView::EntryView(QWidget * _parent)
214 : QLabel(_parent)
215{
216 connect(this, SIGNAL(linkActivated(QString)), SLOT(urlSelected(QString)));
217}
218
219void EntryView::setEntry(Entry *entry)
220{
221 m_entry = entry;
222 buildContents();
223}
224
225void EntryView::updateEntry(Entry *entry)
226{
227 // get item id string and iformations
228 QString idString = QString::number((unsigned long)entry);
229 // AvailableItem::State state = item->state();
230 // bool showProgress = state != AvailableItem::Normal;
231 // int pixelProgress = showProgress ? (int)(item->progress() * 80.0) : 0;
232
233 // perform internal scripting operations over the element
234 // executeScript( "document.getElementById('" + idString + "').style.color='red'" );
235 // executeScript( "document.getElementById('bar" + idString + "').style.width='" +
236 // QString::number( pixelProgress ) + "px'" );
237 // executeScript( "document.getElementById('bc" + idString + "').style.backgroundColor='" +
238 // (showProgress ? "gray" : "transparent") + "'" );
239 // executeScript( "document.getElementById('btn" + idString + "').value='" +
240 // (item->installed() ? i18n( "Uninstall" ) : i18n( "Install" )) + "'" );
241}
242
243void EntryView::buildContents()
244{
245 // write the html header and contents manipulation scripts
246 QString t;
247
248 t += "<html><body>";
249
250 //t += setTheAaronnesqueStyle();
251 // precalc the status icon
252 Entry::Status status = m_entry->status();
253 QString statusIcon;
254 KIconLoader *loader = KIconLoader::global();
255
256 switch (status) {
257 case Entry::Invalid:
258 statusIcon = "<img src='" + loader->iconPath("dialog-error", -KIconLoader::SizeSmall) + "' />";
259 break;
260 case Entry::Downloadable:
261 // find a good icon to represent downloadable data
262 //statusIcon = "<img src='" + loader->iconPath("network-server", -KIconLoader::SizeSmall) + "' />";
263 break;
264 case Entry::Installed:
265 statusIcon = "<img src='" + loader->iconPath("dialog-ok", -KIconLoader::SizeSmall) + "' />";
266 break;
267 case Entry::Updateable:
268 statusIcon = "<img src='" + loader->iconPath("software-update-available", -KIconLoader::SizeSmall) + "' />";
269 break;
270 case Entry::Deleted:
271 statusIcon = "<img src='" + loader->iconPath("user-trash", -KIconLoader::SizeSmall) + "' />";
272 break;
273 }
274
275 // precalc the title string
276 QString titleString = m_entry->name().representation();
277 if (!m_entry->version().isEmpty()) titleString += " v." + Qt::escape(m_entry->version());
278
279 // precalc the string for displaying stars (normal+grayed)
280 QString starIconPath = KStandardDirs::locate("data", "knewstuff/pics/ghns_star.png");
281 QString starBgIconPath = KStandardDirs::locate("data", "knewstuff/pics/ghns_star_gray.png");
282
283 int starPixels = 11 + 11 * (m_entry->rating() / 10);
284 QString starsString = "<div style='width: " + QString::number(starPixels) + "px; background-image: url(" + starIconPath + "); background-repeat: repeat-x;'>&nbsp;</div>";
285 int grayPixels = 22 + 22 * (m_entry->rating() / 20);
286 starsString = "<div style='width: " + QString::number(grayPixels) + "px;background-image: url(" + starBgIconPath + "); background-repeat: repeat-x;'>" + starsString + "&nbsp;</div>";
287
288 // precalc the string for displaying author (parsing email)
289 KNS::Author author = m_entry->author();
290 QString authorString = author.name();
291
292 QString emailString = author.email();
293 if (!emailString.isEmpty()) {
294 authorString = "<a href='mailto:" + Qt::escape(emailString) + "'>"
295 + Qt::escape(authorString) + "</a>";
296 }
297
298 // write the HTML code for the current item
299 t += //QLatin1String("<table class='contentsHeader' cellspacing='2' cellpadding='0'>")
300 statusIcon + Qt::escape(titleString) + "<br />"
301 //+ "<span align='right'>" + starsString + "</span><br />"
302 + Qt::escape(m_entry->summary().representation())
303 + "<br />";
304
305 if (m_entry->rating() > 0) {
306 t += i18n("Rating: ") + QString::number(m_entry->rating())
307 + "<br />";
308 }
309
310 if (m_entry->downloads() > 0) {
311 t += i18n("Downloads: ") + QString::number(m_entry->downloads())
312 + "<br />";
313 }
314
315 if (!authorString.isEmpty()) {
316 t += "<em>" + authorString + "</em>, ";
317 }
318 t += KGlobal::locale()->formatDate(m_entry->releaseDate(), KLocale::ShortDate)
319 + "<br />" + "</body></html>";
320
321 setText(t);
322}
323
324void EntryView::setTheAaronnesqueStyle()
325{
326 QString hoverColor = "#000000"; //QApplication::palette().active().highlightedText().name();
327 QString hoverBackground = "#f8f8f8"; //QApplication::palette().active().highlight().name();
328 QString starIconPath = KStandardDirs::locate("data", "knewstuff/pics/ghns_star.png");
329 QString starBgIconPath = KStandardDirs::locate("data", "knewstuff/pics/ghns_star_gray.png");
330
331 // default elements style
332 QString s;
333 s += "body { background-color: white; color: black; padding: 0; margin: 0; }";
334 s += "table, td, th { padding: 0; margin: 0; text-align: left; }";
335 s += "input { color: #000080; font-size:120%; }";
336
337 // the main item container (custom element)
338 s += ".itemBox { background-color: white; color: black; width: 100%; border-bottom: 1px solid gray; margin: 0px 0px; }";
339 s += ".itemBox:hover { background-color: " + hoverBackground + "; color: " + hoverColor + "; }";
340
341 // s of the item elements (4 cells with multiple containers)
342 s += ".leftColumn { width: 100px; height:100%; text-align: center; }";
343 s += ".leftImage {}";
344 s += ".leftButton {}";
345 s += ".leftProgressContainer { width: 82px; height: 10px; background-color: transparent; }";
346 s += ".leftProgressBar { left: 1px; width: 0px; top: 1px; height: 8px; background-color: red; }";
347 s += ".contentsColumn { vertical-align: top; }";
348 s += ".contentsHeader { width: 100%; font-size: 120%; font-weight: bold; border-bottom: 1px solid #c8c8c8; }";
349 s += ".contentsBody {}";
350 s += ".contentsFooter {}";
351 s += ".star { width: 0px; height: 24px; background-image: url(" + starIconPath + "); background-repeat: repeat-x; }";
352 s += ".starbg { width: 110px; height: 24px; background-image: url(" + starBgIconPath + "); background-repeat: repeat-x; }";
353 setStyleSheet(s);
354}
355
356void EntryView::urlSelected(const QString &link)
357{
358 //kDebug() << "Clicked on URL " << link;
359
360 KUrl url(link);
361 QString urlProtocol = url.protocol();
362 QString urlPath = url.path();
363
364 if (urlProtocol == "mailto") {
365 // clicked over a mail address
366 // FIXME: if clicked with MRB, show context menu with IM etc.
367 // FIXME: but RMB never reaches this method?!
368 KToolInvocation::invokeMailer(url);
369 } else if (urlProtocol == "item") {
370 // clicked over an item
371 bool ok;
372 unsigned long itemPointer = urlPath.toULong(&ok);
373 if (!ok) {
374 kWarning() << "ItemsView: error converting item pointer.";
375 return;
376 }
377
378 // I love to cast pointers
379 Entry *entry = (Entry*)itemPointer;
380 if (entry != m_entry) {
381 kWarning() << "ItemsView: error retrieving item pointer.";
382 return;
383 }
384
385 // XXX ???
386 // install/uninstall the item
387 // if ( item->installed() )
388 // m_newStuffDialog->removeItem( item ); // synchronous
389 // else
390 // m_newStuffDialog->installItem( item ); // asynchronous
391 }
392}
393
394#include "itemsview.moc"
395
category.h
KIconLoader
KIconLoader::global
static KIconLoader * global()
KIconLoader::SizeSmall
SizeSmall
KIconLoader::iconPath
QString iconPath(const QString &name, int group_or_size, bool canReturnNull=false) const
KLocale::formatDate
QString formatDate(const QDate &date, DateFormat format=LongDate) const
KLocale::ShortDate
ShortDate
KNS::Author
KNewStuff author information.
Definition: knewstuff2/core/author.h:41
KNS::Author::name
QString name() const
Retrieve the author's name.
Definition: knewstuff2/core/author.cpp:58
KNS::Author::email
QString email() const
Retrieve the author's email address.
Definition: knewstuff2/core/author.cpp:68
KNS::DxsEngine
KNewStuff DXS engine.
Definition: dxsengine.h:40
KNS::EntryView
GUI/CORE: HTML Widget for exactly one AvailableItem::List.
Definition: knewstuff2/ui/itemsview.h:97
KNS::EntryView::EntryView
EntryView(QWidget *parentWidget)
Definition: knewstuff2/ui/itemsview.cpp:213
KNS::EntryView::updateEntry
void updateEntry(Entry *entry)
Definition: knewstuff2/ui/itemsview.cpp:225
KNS::EntryView::setEntry
void setEntry(Entry *entry)
Definition: knewstuff2/ui/itemsview.cpp:219
KNS::Entry
KNewStuff data entry container.
Definition: knewstuff2/core/entry.h:47
KNS::Entry::preview
KTranslatable preview() const
Retrieve the file name of an image containing a preview of the object.
Definition: knewstuff2/core/entry.cpp:171
KNS::Entry::releaseDate
QDate releaseDate() const
Retrieve the date of the object's publication.
Definition: knewstuff2/core/entry.cpp:151
KNS::Entry::name
KTranslatable name() const
Retrieve the name of the data object.
Definition: knewstuff2/core/entry.cpp:81
KNS::Entry::downloads
int downloads() const
Retrieve the download count for the object, which has been determined by its hosting sites and thus m...
Definition: knewstuff2/core/entry.cpp:191
KNS::Entry::status
Status status()
Retrieves the entry's status.
Definition: knewstuff2/core/entry.cpp:216
KNS::Entry::version
QString version() const
Retrieve the version string of the object.
Definition: knewstuff2/core/entry.cpp:131
KNS::Entry::Status
Status
Status of the entry.
Definition: knewstuff2/core/entry.h:290
KNS::Entry::Invalid
@ Invalid
Definition: knewstuff2/core/entry.h:291
KNS::Entry::Installed
@ Installed
Definition: knewstuff2/core/entry.h:293
KNS::Entry::Deleted
@ Deleted
Definition: knewstuff2/core/entry.h:295
KNS::Entry::Updateable
@ Updateable
Definition: knewstuff2/core/entry.h:294
KNS::Entry::Downloadable
@ Downloadable
Definition: knewstuff2/core/entry.h:292
KNS::Entry::summary
KTranslatable summary() const
Retrieve a short description about the object.
Definition: knewstuff2/core/entry.cpp:121
KNS::Entry::rating
int rating() const
Retrieve the rating for the object, which has been determined by its users and thus might change over...
Definition: knewstuff2/core/entry.cpp:181
KNS::Entry::author
Author author() const
Retrieve the author of the object.
Definition: knewstuff2/core/entry.cpp:101
KNS::Feed
KNewStuff feed.
Definition: feed.h:46
KNS::Feed::entries
Entry::List entries() const
Retrieves the list of associated entries.
Definition: feed.cpp:84
KNS::ItemsView::setEngine
void setEngine(DxsEngine *engine)
Definition: knewstuff2/ui/itemsview.cpp:108
KNS::ItemsView::~ItemsView
~ItemsView()
Definition: knewstuff2/ui/itemsview.cpp:104
KNS::ItemsView::ItemsView
ItemsView(QWidget *parentWidget)
Definition: knewstuff2/ui/itemsview.cpp:94
KNS::ItemsView::setProvider
void setProvider(const Provider *provider, const Feed *feed)
set the provider to show entries from with the feed also
Definition: knewstuff2/ui/itemsview.cpp:113
KNS::ItemsView::setFeed
void setFeed(const Feed *)
set which feed from the current provider to show
Definition: knewstuff2/ui/itemsview.cpp:126
KNS::ItemsView::updateItem
void updateItem(Entry *entry)
update the given item because it has changed
Definition: knewstuff2/ui/itemsview.cpp:138
KNS::ItemsView::setSorting
void setSorting(int sortType)
Definition: knewstuff2/ui/itemsview.cpp:120
KNS::ItemsView::setSearchText
void setSearchText(const QString &text)
set the search text to filter the shown entries by
Definition: knewstuff2/ui/itemsview.cpp:132
KNS::KDXSButton
KNewStuff DXS interaction button.
Definition: kdxsbutton.h:50
KNS::KDXSButton::setEngine
void setEngine(KNS::DxsEngine *engine)
Definition: kdxsbutton.cpp:200
KNS::KDXSButton::setProvider
void setProvider(const KNS::Provider *provider)
Definition: kdxsbutton.cpp:166
KNS::KDXSButton::setEntry
void setEntry(KNS::Entry *e)
Definition: kdxsbutton.cpp:124
KNS::KTranslatable::representation
QString representation() const
Returns the string which matches most closely the current language.
Definition: ktranslatable.cpp:64
KNS::Provider
KNewStuff provider container.
Definition: knewstuff2/core/provider.h:52
KStandardDirs::locate
static QString locate(const char *type, const QString &filename, const KComponentData &cData=KGlobal::mainComponent())
KToolInvocation::invokeMailer
static void invokeMailer(const KUrl &mailtoURL, const QByteArray &startup_id=QByteArray(), bool allowAttachments=false)
KUrl
QAsyncImage
Convenience class for images with remote sources.
Definition: qasyncimage_p.h:45
QLabel
QListView
QList< Entry * >
QWidget
dxs.h
entryhandler.h
kWarning
#define kWarning
job.h
kaboutdata.h
kapplication.h
kcomponentdata.h
kconfig.h
kdebug.h
kdxsbutton.h
kglobalsettings.h
kiconloader.h
klineedit.h
klocale.h
i18n
QString i18n(const char *text)
kmessagebox.h
entry.h
provider.h
RatingSorter
static bool RatingSorter(const Entry *e1, const Entry *e2)
Definition: knewstuff2/ui/itemsview.cpp:77
RecentSorter
static bool RecentSorter(const Entry *e1, const Entry *e2)
Definition: knewstuff2/ui/itemsview.cpp:82
DownloadsSorter
static bool DownloadsSorter(const Entry *e1, const Entry *e2)
Definition: knewstuff2/ui/itemsview.cpp:88
NameSorter
static bool NameSorter(const Entry *e1, const Entry *e2)
Definition: knewstuff2/ui/itemsview.cpp:72
itemsview.h
kstandarddirs.h
ktitlewidget.h
ktoolinvocation.h
KGlobal::locale
KLocale * locale()
KNS
Definition: knewstuff2/core/author.h:27
ok
KGuiItem ok()
netaccess.h
providerhandler.h
qasyncimage_p.h
qprogressindicator.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KNewStuff

Skip menu "KNewStuff"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal