[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: playground/base/plasmagik
From: Joseph Burns <jburns05.phate () gmail ! com>
Date: 2008-08-31 21:37:56
Message-ID: 1220218676.420966.26149.nullmailer () svn ! kde ! org
[Download RAW message or body]
SVN commit 855477 by josebur:
New delegate class to draw the items nicer than just a boring treeview
M +3 -0 item.h
M +107 -78 structuredelegate.cpp
M +6 -28 structuredelegate.h
M +3 -10 structuremodel.cpp
M +4 -3 structuremodel.h
M +14 -0 structureparser.cpp
M +2 -0 structureparser.h
--- trunk/playground/base/plasmagik/item.h #855476:855477
@@ -5,6 +5,8 @@
#include <QList>
#include <QStringList>
+#include <KIcon>
+
class Item
{
public:
@@ -33,6 +35,7 @@
QString userPath;
QString name;
QStringList mimetypes;
+ KIcon icon;
Item *parent;
QList<Item *> children;
--- trunk/playground/base/plasmagik/structuredelegate.cpp #855476:855477
@@ -1,8 +1,10 @@
-#include <QWidget>
-#include <QHBoxLayout>
-#include <QLabel>
-#include <QToolButton>
+#include <QPainter>
#include <QVariant>
+#include <QSize>
+#include <QFont>
+#include <QPixmap>
+#include <QColor>
+#include <QLinearGradient>
#include <KIcon>
#include <KFileDialog>
@@ -12,101 +14,128 @@
#include "item.h"
#include "structuredelegate.h"
-EditorWidget::EditorWidget(QWidget *parent)
- : QWidget(parent)
+#define UNIVERSAL_PADDING 6
+#define MAIN_ICON_SIZE 48
+#define FADE_LENGTH 32
+
+StructureDelegate::StructureDelegate(QWidget *parent)
+ : QAbstractItemDelegate(parent)
{
- layout = new QHBoxLayout();
-
- label = new QLabel();
- button = new QToolButton();
- button->setMinimumSize(22, 22);
- button->setIcon(KIcon("document-open"));
-
- layout->addWidget(label);
- layout->addWidget(button);
- layout->setSizeConstraint(QLayout::SetMinimumSize);
- this->setLayout(layout);
- this->setFocusProxy(label);
}
-StructureDelegate::StructureDelegate(QWidget *parent)
- : QItemDelegate(parent)
+int StructureDelegate::calcItemHeight(const QStyleOptionViewItem &option) const
{
+ // Painting main column
+ QFont titleFont = option.font;
+ titleFont.setBold(true);
+ titleFont.setPointSize(titleFont.pointSize() + 2);
+ int textHeight = QFontInfo(titleFont).pixelSize() + \
QFontInfo(option.font).pixelSize(); + return qMax(textHeight, MAIN_ICON_SIZE) + 2 \
* UNIVERSAL_PADDING; }
-QWidget* StructureDelegate::createEditor(QWidget *parent,
- const QStyleOptionViewItem &option,
- const QModelIndex &index) const
+
+void StructureDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, \
const QModelIndex &index) const {
- if (index.data(Qt::UserRole) == Item::File) {
- EditorWidget *w = new EditorWidget(parent);
- connect(w->button, SIGNAL(clicked()),
- this, SLOT(getFileData()));
- w->mimetypes = index.data(Qt::UserRole + 3).toStringList();
- return w;
- } else if (index.data(Qt::UserRole) == Item::Directory){
- EditorWidget *w = new EditorWidget(parent);
- connect(w->button, SIGNAL(clicked()),
- this, SLOT(getDirectoryData()));
- w->mimetypes = index.data(Qt::UserRole + 3).toStringList();
- return w;
+ const int left = option.rect.left();
+ const int top = option.rect.top();
+ const int width = option.rect.width();
+ const int height = calcItemHeight(option);
+
+ bool leftToRight = (painter->layoutDirection() == Qt::LeftToRight);
+ QIcon::Mode iconMode = QIcon::Normal;
+ QColor foregroundColor = option.palette.color(QPalette::Text);
+
+ // Painting main column
+ QFont titleFont = option.font;
+ titleFont.setBold(true);
+ titleFont.setPointSize(titleFont.pointSize() + 2);
+
+ QPixmap pixmap(width, height);
+ pixmap.fill(Qt::transparent);
+ QPainter p(&pixmap);
+ p.translate(-option.rect.topLeft());
+
+ QLinearGradient gradient;
+
+ QString name = index.data(Qt::UserRole + 1).toString();
+ QString userPath = index.data(Qt::DisplayRole).toString();
+
+ // Painting
+
+ // Text
+ int textInner = 2 * UNIVERSAL_PADDING + MAIN_ICON_SIZE;
+
+ p.setPen(foregroundColor);
+ p.setFont(titleFont);
+ p.drawText(left + (leftToRight ? textInner : 0),
+ top, width - textInner, height / 2,
+ Qt::AlignBottom | Qt::AlignLeft, name);
+ p.setFont(option.font);
+ p.drawText(left + (leftToRight ? textInner : 0),
+ top + height / 2,
+ width - textInner, height / 2,
+ Qt::AlignTop | Qt::AlignLeft, userPath);
+
+ // Main icon
+
+// index.icon(index).paint(&p,
+// leftToRight ? left + UNIVERSAL_PADDING : left + width - \
UNIVERSAL_PADDING - MAIN_ICON_SIZE, +// top + UNIVERSAL_PADDING,
+// MAIN_ICON_SIZE, MAIN_ICON_SIZE, Qt::AlignCenter, iconMode);
+
+ // Gradient part of the background - fading of the text at the end
+ if (leftToRight) {
+ gradient = QLinearGradient(left + width - UNIVERSAL_PADDING - FADE_LENGTH, \
0, + left + width - UNIVERSAL_PADDING, 0);
+ gradient.setColorAt(0, Qt::white);
+ gradient.setColorAt(1, Qt::transparent);
} else {
- return QItemDelegate::createEditor(parent, option, index);
+ gradient = QLinearGradient(left + UNIVERSAL_PADDING, 0,
+ left + UNIVERSAL_PADDING + FADE_LENGTH, 0);
+ gradient.setColorAt(0, Qt::transparent);
+ gradient.setColorAt(1, Qt::white);
}
-}
-void StructureDelegate::setEditorData(QWidget *editor, const QModelIndex &index) \
const
-{
- if (index.data(Qt::UserRole) == Item::File) {
- EditorWidget *widget = qobject_cast<EditorWidget *>(editor);
- QString path = index.data().toString();
- //widget->label->setText(path);
- } else {
- QItemDelegate::setEditorData(editor, index);
- }
+ QRect paintRect = option.rect;
+ p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
+ p.fillRect(paintRect, gradient);
+
+ painter->drawPixmap(option.rect.topLeft(), pixmap);
}
-void StructureDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
- const QModelIndex &index) const
+QSize StructureDelegate::sizeHint(const QStyleOptionViewItem &option, const \
QModelIndex &index) const {
- if (index.column() == 1) {
- EditorWidget *widget = qobject_cast<EditorWidget *>(editor);
- if (widget->label->text() != QString()) {
- model->setData(index, QVariant(widget->label->text()));
- }
- } else {
- QItemDelegate::setModelData(editor, model, index);
- }
+ return QSize(0, calcItemHeight(option));
}
void StructureDelegate::getFileData()
{
- QToolButton *button = qobject_cast<QToolButton *>(sender());
- EditorWidget *editor = qobject_cast<EditorWidget *>(button->parentWidget());
- QLabel *label = editor->label;
-
- QString path = KFileDialog::getOpenFileName(KUrl(), editor->mimetypes.join(" \
"));
- if (!path.isEmpty()) {
- label->setText(path);
- }
-
- emit commitData(editor);
- emit closeEditor(editor);
+// QToolButton *button = qobject_cast<QToolButton *>(sender());
+// EditorWidget *editor = qobject_cast<EditorWidget *>(button->parentWidget());
+// QLabel *label = editor->label;
+//
+// QString path = KFileDialog::getOpenFileName(KUrl(), editor->mimetypes.join(" \
")); +// if (!path.isEmpty()) {
+// label->setText(path);
+// }
+//
+// emit commitData(editor);
+// emit closeEditor(editor);
}
void StructureDelegate::getDirectoryData()
{
- QToolButton *button = qobject_cast<QToolButton *>(sender());
- EditorWidget *editor = qobject_cast<EditorWidget *>(button->parentWidget());
- QLabel *label = editor->label;
-
- QString path = KFileDialog::getExistingDirectory(KUrl());
- if (!path.isEmpty()) {
- label->setText(path);
- }
-
- emit commitData(editor);
- emit closeEditor(editor);
+// QToolButton *button = qobject_cast<QToolButton *>(sender());
+// EditorWidget *editor = qobject_cast<EditorWidget *>(button->parentWidget());
+// QLabel *label = editor->label;
+//
+// QString path = KFileDialog::getExistingDirectory(KUrl());
+// if (!path.isEmpty()) {
+// label->setText(path);
+// }
+//
+// emit commitData(editor);
+// emit closeEditor(editor);
}
--- trunk/playground/base/plasmagik/structuredelegate.h #855476:855477
@@ -3,41 +3,19 @@
#include <QWidget>
#include <QModelIndex>
-#include <QItemDelegate>
+#include <QAbstractItemDelegate>
-
-class QWidget;
-class QAbstractItemModel;
-class QHBoxLayout;
-class QToolButton;
-class QLabel;
-
-class EditorWidget : public QWidget
+class StructureDelegate : public QAbstractItemDelegate
{
Q_OBJECT
public:
- EditorWidget(QWidget *parent = 0);
-
-
- QHBoxLayout *layout;
- QToolButton *button;
- QLabel *label;
- QStringList mimetypes;
-};
-
-class StructureDelegate : public QItemDelegate
-{
- Q_OBJECT
-
- public:
StructureDelegate(QWidget *parent = 0);
- QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option,
- const QModelIndex &index) const;
- void setEditorData(QWidget *editor, const QModelIndex &index) const;
- void setModelData(QWidget *editor, QAbstractItemModel *model,
- const QModelIndex &index) const;
+ int calcItemHeight(const QStyleOptionViewItem &option) const;
+ void paint(QPainter *painter, const QStyleOptionViewItem &option, const \
QModelIndex &index) const; + QSize sizeHint(const QStyleOptionViewItem \
&option, const QModelIndex &index) const; +
private slots:
void getFileData();
void getDirectoryData();
--- trunk/playground/base/plasmagik/structuremodel.cpp #855476:855477
@@ -79,7 +79,7 @@
int StructureModel::columnCount(const QModelIndex &/*parent*/) const
{
- return 2;
+ return 1;
}
QVariant StructureModel::data(const QModelIndex &index, int role) const
@@ -110,6 +110,8 @@
return item->required;
} else if (role == Qt::UserRole + 3) {
return item->mimetypes;
+ } else if (role == Qt::UserRole + 4) {
+ return item->icon;
}
return QVariant();
}
@@ -198,15 +200,6 @@
return success;
}
-// QModelIndex StructureModel::buddy(const QModelIndex &index) const
-// {
-// if (index.column() == 0) {
-// return this->index(index.row(), 1, index.parent());
-// } else {
-// return index;
-// }
-// }
-
bool StructureModel::hasAllRequiredItems()
{
return checkRequiredItems(rootItem);
--- trunk/playground/base/plasmagik/structuremodel.h #855476:855477
@@ -13,6 +13,7 @@
#include <KUrl>
#include "plasma/packagestructure.h"
+#include "structuredelegate.h"
class Item;
@@ -34,18 +35,18 @@
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) \
const;
-
+
Qt::ItemFlags flags(const QModelIndex &index) const;
bool setData(const QModelIndex &index, const QVariant &value, int role = \
Qt::EditRole);
bool insertRows(int row, int count, const QModelIndex &parent = \
QModelIndex());
bool removeRows(int row, int count, const QModelIndex &parent = \
QModelIndex());
- //QModelIndex buddy(const QModelIndex &index) const;
-
bool hasAllRequiredItems();
QMap<QString, QString> getFilePaths();
+
+
public slots:
void slotListEntries(KIO::Job *job, const KIO::UDSEntryList &list);
--- trunk/playground/base/plasmagik/structureparser.cpp #855476:855477
@@ -49,6 +49,7 @@
if (!child) {
child = new Item(type, required, name, currentPath);
child->mimetypes = structure->mimetypes(key.toAscii());
+ child->icon = getIcon(child);
topLevelItems.append(child);
}
} else {
@@ -56,6 +57,7 @@
if (!child) {
child = new Item(type, required, name, currentPath);
child->mimetypes = structure->mimetypes(key.toAscii());
+ child->icon = getIcon(child);
parent->children.append(child);
child->parent = parent;
}
@@ -83,3 +85,15 @@
return 0;
}
}
+
+KIcon StructureParser::getIcon(Item *item)
+{
+ if (item->mimetypes.count() > 0) {
+ QString type = item->mimetypes.at(0);
+ kDebug() << type;
+ type.replace("/", "-");
+ return KIcon(type);
+ } else {
+ return KIcon("unknown");
+ }
+}
\ No newline at end of file
--- trunk/playground/base/plasmagik/structureparser.h #855476:855477
@@ -17,6 +17,8 @@
void addItem(Item::Type type, bool required, const QString &name, const \
QString &key); Item* searchChildren(Item *parent, const QString &item);
+ KIcon getIcon(Item *item);
+
Plasma::PackageStructure *structure;
QList<Item *> topLevelItems;
};
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic