Commit 304249a3 authored by Jarosław Staniek's avatar Jarosław Staniek
Browse files

KoProperty

- fixed groups order, API changed: now it's possible to get order of groups
- sorting disabled when groups are present
- icons can be defined and displayed within the group items

2.0: ported


svn path=/trunk/koffice/; revision=546068
parent 67ca28eb
/* This file is part of the KDE project
Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
Copyright (C) 2004 Alexander Dymo <cloudtemple@mskat.net>
Copyright (C) 2004-2005 Jaroslaw Staniek <js@iidea.pl>
Copyright (C) 2004-2006 Jaroslaw Staniek <js@iidea.pl>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
......@@ -176,7 +176,8 @@ Editor::Editor(QWidget *parent, bool autoSync, const char *name)
connect(this, SIGNAL(expanded(Q3ListViewItem *)), this, SLOT(slotExpanded(Q3ListViewItem *)));
connect(this, SIGNAL(collapsed(Q3ListViewItem *)), this, SLOT(slotCollapsed(Q3ListViewItem *)));
connect(header(), SIGNAL(sizeChange(int, int, int)), this, SLOT(slotColumnSizeChanged(int, int, int)));
connect(header(), SIGNAL(clicked(int)), this, SLOT(updateEditorGeometry()));
// connect(header(), SIGNAL(clicked(int)), this, SLOT(updateEditorGeometry()));
// connect(header(), SIGNAL(clicked(int)), this, SLOT(updateEditorGeometryAndGroupLabels()));
connect(header(), SIGNAL(sectionHandleDoubleClicked (int)), this, SLOT(slotColumnSizeChanged(int)));
}
......@@ -204,29 +205,36 @@ Editor::fill()
d->topItem = new EditorDummyItem(this);
// int i = 0;
StringListMap map = d->set->groups();
// kopropertydbg << "Editor::fill(): groups = " << map.count() << endl;
if(map.count() == 1) { // one group (default one), so don't show groups
// EditorGroupItem *hiddenGroupItem = new EditorGroupItem(d->topItem, "");
Q3ValueList<QByteArray> props = map.begin().data();
Q3ValueList<QByteArray>::ConstIterator it = props.constBegin();
for( ; it != props.constEnd(); ++it)
const Q3ValueList<QByteArray> groupNames = d->set->groupNames();
// kopropertydbg << "Editor::fill(): group names = " << groupNames.count() << endl;
if(groupNames.count() == 1) { // one group (default one), so don't show groups
//add flat set of properties
const Q3ValueList<QByteArray>& propertyNames = d->set->propertyNamesForGroup( groupNames.first() );
Q3ValueListConstIterator<QByteArray> it = propertyNames.constBegin();
for( ; it != propertyNames.constEnd(); ++it)
addItem(*it, d->topItem);
}
else { // create a groupItem for each group
EditorGroupItem *prevGroupItem = 0;
int sortOrder = 0;
for (Q3ValueListConstIterator<QByteArray> it = groupNames.constBegin(); it!=groupNames.constEnd();
++it, sortOrder++)
{
const Q3ValueList<QByteArray>& propertyNames = d->set->propertyNamesForGroup(*it);
EditorGroupItem *groupItem;
if (prevGroupItem)
groupItem = new EditorGroupItem(d->topItem, prevGroupItem,
d->set->groupDescription(*it), d->set->groupIcon(*it), sortOrder );
else
groupItem = new EditorGroupItem(d->topItem,
d->set->groupDescription(*it), d->set->groupIcon(*it), sortOrder );
} else { // else create a groupItem for each group
StringListMap::ConstIterator it = map.constBegin();
for( ; it != map.constEnd(); ++it) {
EditorGroupItem *groupItem = 0;
if(!it.key().isEmpty() && !it.data().isEmpty() && map.count() > 1)
groupItem = new EditorGroupItem(d->topItem, d->set->groupDescription(it.key()) );
Q3ValueList<QByteArray>::ConstIterator it2 = it.data().constBegin();
for( ; it2 != it.data().constEnd(); ++it2)
Q3ValueList<QByteArray>::ConstIterator it2 = propertyNames.constBegin();
for( ; it2 != propertyNames.constEnd(); ++it2)
addItem(*it2, groupItem);
}
prevGroupItem = groupItem;
}
}
// repaint();
......@@ -759,6 +767,8 @@ Editor::slotExpanded(Q3ListViewItem *item)
}
updateEditorGeometry();
updateGroupLabelsPosition();
repaintContents();
repaint();
}
void
......@@ -781,6 +791,8 @@ Editor::slotCollapsed(Q3ListViewItem *item)
}
updateEditorGeometry();
updateGroupLabelsPosition();
repaintContents();
repaint();
}
void
......@@ -789,7 +801,6 @@ Editor::slotColumnSizeChanged(int section, int oldSize, int newSize)
Q_UNUSED(section);
Q_UNUSED(oldSize);
Q_UNUSED(newSize);
updateEditorGeometry();
/*for (Q3ListViewItemIterator it(this); it.current(); ++it) {
// if (section == 0 && dynamic_cast<EditorGroupItem*>(it.current())) {
// it.current()->repaint();
......@@ -808,6 +819,9 @@ Editor::slotColumnSizeChanged(int section, int oldSize, int newSize)
d->currentWidget->height());
}
}*/
// repaintContents();
// repaint();
updateEditorGeometry();
update();
}
......@@ -975,4 +989,16 @@ Editor::contentsMousePressEvent( QMouseEvent * e )
K3ListView::contentsMousePressEvent(e);
}
void
Editor::setSorting( int column, bool ascending )
{
if (d->set && d->set->groupNames().count()>1) //do not sort when groups are present (maybe reenable this later?)
return;
KListView::setSorting( column, ascending );
updateEditorGeometry();
updateGroupLabelsPosition();
repaintContents();
repaint();
}
#include "editor.moc"
/* This file is part of the KDE project
Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
Copyright (C) 2004 Alexander Dymo <cloudtemple@mskat.net>
Copyright (C) 2004-2005 Jaroslaw Staniek <js@iidea.pl>
Copyright (C) 2004-2006 Jaroslaw Staniek <js@iidea.pl>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
......@@ -82,6 +82,7 @@ class KOPROPERTY_EXPORT Editor : public K3ListView
virtual QSize sizeHint() const;
virtual void setFocus();
virtual void setSorting( int column, bool ascending = TRUE );
public slots:
/*! Populates the editor with an item for each property in the List.
......
/* This file is part of the KDE project
Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
Copyright (C) 2004 Alexander Dymo <cloudtemple@mskat.net>
Copyright (C) 2004-2005 Jaroslaw Staniek <js@iidea.pl>
Copyright (C) 2004-2006 Jaroslaw Staniek <js@iidea.pl>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
......@@ -122,24 +122,22 @@ class GroupWidget : public QWidget
qApp->style()->drawControl(QStyle::CE_HeaderSection, &so, &p, this);
if (miniicon.isNull()) {
paintListViewExpander(&p, this, r.height(), palette().active(), m_parentItem->isOpen());
}
else {
p.drawPixmap(4, (r.height()-miniicon.height())/2, miniicon);
paintListViewExpander(&p, this, r.height()+2, palette().active(), m_parentItem->isOpen());
if (!miniicon.isNull()) {
p.drawPixmap(24, (r.height()-miniicon.height())/2, miniicon);
}
if (!titleStr.isNull())
{
int indent = miniicon.isNull() ? 16 : miniicon.width();
int indent = 16 + (miniicon.isNull() ? 0 : (miniicon.width()+4));
p.setPen(palette().active().text());
QFont f = p.font();
f.setBold(true);
p.setFont(f);
p.drawText(indent+8, 0, width()-(indent+8), height(), Qt::TextSingleLine | Qt::AlignLeft | Qt::AlignVCenter, titleStr);
}
//todo p.setPen(palette().active().mid());
//todo p.drawLine(0, 0, r.right(), 0);
// p.setPen(palette().active().mid());
// p.drawLine(0, 0, r.right(), 0);
}
QString titleStr;
QPixmap miniicon;
......@@ -194,6 +192,15 @@ EditorItem::EditorItem(EditorItem *parent, const QString &text)
setMultiLinesEnabled(true);
}
EditorItem::EditorItem(EditorItem *parent, EditorItem *after, const QString &text)
: KListViewItem(parent, after, text)
{
d = new EditorItemPrivate();
d->property = 0;
d->editor = 0;
setMultiLinesEnabled(true);
}
EditorItem::~EditorItem()
{
delete d;
......@@ -264,7 +271,7 @@ EditorItem::paintCell(QPainter *p, const QColorGroup & cg, int column, int width
#endif
Widget *widget = d->editor->createWidgetForProperty(d->property, false /*don't change Widget::property() */);
if(widget) {
QRect r(0, 0, d->editor->header()->sectionSize(1), height() - (widget->hasBorders() ? 1 : 2));
QRect r(0, 0, d->editor->header()->sectionSize(1), height() - (widget->hasBorders() ? 0 : 1));
p->setClipRect(r);
p->setClipping(true);
widget->drawViewer(p, icg, r, d->property->value());
......@@ -374,7 +381,7 @@ EditorItem::paintBranches(QPainter *p, const QColorGroup &cg, int w, int y, int
//int margin = listView()->itemMargin();
QPixmap pix = SmallIcon(editorItem->property()->icon());
if (!pix.isNull())
p->drawPixmap(1, (item->height() - pix.height()) / 2, pix);
p->drawPixmap(-19+(19-pix.width())/2, (item->height() - pix.height()) / 2, pix);
}
p->translate(0, item->totalHeight());
......@@ -389,7 +396,8 @@ EditorItem::paintBranches(QPainter *p, const QColorGroup &cg, int w, int y, int
void
EditorItem::paintFocus(QPainter *, const QColorGroup &, const QRect & )
{}
{
}
int
EditorItem::compare( Q3ListViewItem *i, int col, bool ascending ) const
......@@ -402,7 +410,8 @@ EditorItem::compare( Q3ListViewItem *i, int col, bool ascending ) const
// << static_cast<EditorItem*>(i)->property()->name() << " "
// << static_cast<EditorItem*>(i)->property()->sortingKey() << endl;
return d->property->sortingKey()
- ((dynamic_cast<EditorItem*>(i) && dynamic_cast<EditorItem*>(i)->property()) ? dynamic_cast<EditorItem*>(i)->property()->sortingKey() : 0);
- ((dynamic_cast<EditorItem*>(i) && dynamic_cast<EditorItem*>(i)->property())
? dynamic_cast<EditorItem*>(i)->property()->sortingKey() : 0);
}
return 0;
......@@ -417,14 +426,20 @@ EditorItem::setHeight( int height )
//////////////////////////////////////////////////////
EditorGroupItem::EditorGroupItem(EditorItem *parent, const QString &text)
: EditorItem(parent, text), m_label(0)
EditorGroupItem::EditorGroupItem(EditorItem *parent, EditorItem *after, const QString &text, const QString &icon, int sortOrder)
: EditorItem(parent, after, text)
, m_label(0)
, m_sortOrder(sortOrder)
{
setOpen(true);
setSelectable(false);
m_label = new GroupWidget(this);
m_label->setText(text); //todo: icon?
m_label->show();
init(icon);
}
EditorGroupItem::EditorGroupItem(EditorItem *parent, const QString &text, const QString &icon, int sortOrder)
: EditorItem(parent, text)
, m_label(0)
, m_sortOrder(sortOrder)
{
init(icon);
}
EditorGroupItem::~EditorGroupItem()
......@@ -437,10 +452,23 @@ QWidget* EditorGroupItem::label() const
return m_label;
}
void EditorGroupItem::init(const QString &icon)
{
setOpen(true);
setSelectable(false);
m_label = new GroupWidget(this);
m_label->setText(text(0)); //todo: icon?
if (!icon.isEmpty())
m_label->setIcon( SmallIcon(icon) );
m_label->show();
}
void
EditorGroupItem::paintCell(QPainter *p, const QColorGroup & cg, int column, int /*width*/, int /*align*/)
EditorGroupItem::paintCell(QPainter *p, const QColorGroup & cg, int column, int width, int /*align*/)
{
//no need to draw anything since there's a label on top of it
// p->fillRect(0, 0, width, height(), cg.base());
//if(column == 1)
// return;
/*p->setPen( KPROPEDITOR_ITEM_BORDER_COLOR ); //! \todo custom color?
......@@ -475,6 +503,16 @@ EditorGroupItem::setup()
setHeight( height()+4 );
}
int
EditorGroupItem::compare( QListViewItem *i, int col, bool ascending ) const
{
if (dynamic_cast<EditorGroupItem*>(i)) {
return m_sortOrder
- dynamic_cast<EditorGroupItem*>(i)->m_sortOrder;
}
return 0;
}
////////////////////////////////////////////////////////
EditorDummyItem::EditorDummyItem(K3ListView *listview)
......
/* This file is part of the KDE project
Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
Copyright (C) 2004 Alexander Dymo <cloudtemple@mskat.net>
Copyright (C) 2004-2005 Jaroslaw Staniek <js@iidea.pl>
Copyright (C) 2004-2006 Jaroslaw Staniek <js@iidea.pl>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
......@@ -63,6 +63,7 @@ class EditorItem : public K3ListViewItem
//! Two helper contructors for subclass
EditorItem(K3ListView *parent);
EditorItem(EditorItem *parent, const QString &text);
EditorItem(EditorItem *parent, EditorItem *after, const QString &text);
virtual ~EditorItem();
......@@ -94,18 +95,25 @@ class EditorItem : public K3ListViewItem
class EditorGroupItem : public EditorItem
{
public:
EditorGroupItem(EditorItem *parent, const QString &text);
EditorGroupItem(EditorItem *parent, EditorItem *after, const QString &text,
const QString &icon, int sortOrder);
EditorGroupItem(EditorItem *parent, const QString &text,
const QString &icon, int sortOrder);
virtual ~EditorGroupItem();
// void setLabel(QLabel *label) { m_label = label; }
QWidget* label() const;
protected:
virtual void init(const QString &icon);
/*! Reimplemented from K3ListViewItem to draw custom contents. */
virtual void paintCell(QPainter *p, const QColorGroup & cg, int column, int width, int align);
virtual void setup();
virtual int compare( QListViewItem *i, int col, bool ascending ) const;
GroupWidget *m_label;
GroupWidget *m_label;
int m_sortOrder;
};
//! @internal
......
......@@ -36,6 +36,9 @@
#include <klocale.h>
#endif
typedef QMap<QByteArray, Q3ValueList<QByteArray> > StringListMap;
typedef QMapIterator<QByteArray, QStringList> StringListMapIterator;
namespace KoProperty {
//! @internal
......@@ -54,7 +57,7 @@ class SetPrivate
//groups of properties:
// list of group name: (list of property names)
StringListMap propertiesOfGroup;
QMap<QByteArray, QString> groupsDescription;
QMap<QByteArray, QString> groupDescriptions;
// map of property: group
QMap<Property*, QByteArray> groupForProperty;
......@@ -126,7 +129,7 @@ Set::Set(QObject *parent, const QString &typeName)
{
d = new SetPrivate();
d->ownProperty = true;
d->groupsDescription.insert("common", i18nc("General properties", "General"));
d->groupDescriptions.insert("common", i18nc("General properties", "General"));
d->typeName = typeName;
}
......@@ -143,7 +146,7 @@ Set::Set(bool propertyOwner)
{
d = new SetPrivate();
d->ownProperty = propertyOwner;
d->groupsDescription.insert("common", i18nc("General properties", "General"));
d->groupDescriptions.insert("common", i18nc("General properties", "General"));
}
Set::~Set()
......@@ -219,6 +222,7 @@ Set::clear()
{
aboutToBeCleared();
d->propertiesOfGroup.clear();
d->groupNames.clear();
Property::DictIterator it(d->dict);
while (it.current())
removeProperty( it.current() );
......@@ -240,6 +244,7 @@ Set::addToGroup(const QByteArray &group, Property *property)
Q3ValueList<QByteArray> l;
l.append(property->name());
d->propertiesOfGroup.insert(group, l);
d->groupNames.append(group);
}
else {
d->propertiesOfGroup[group].append(property->name());
......@@ -254,29 +259,56 @@ Set::removeFromGroup(Property *property)
return;
QByteArray group = d->groupForProperty[property];
d->propertiesOfGroup[group].remove(property->name());
if (d->propertiesOfGroup[group].isEmpty()) {
//remove group as well
d->propertiesOfGroup.remove(group);
Q3ValueListIterator<QByteArray> it = d->groupNames.find(group);
if (it != d->groupNames.end()) {
d->groupNames.remove(it);
}
}
d->groupForProperty.remove(property);
}
const StringListMap&
Set::groups()
const Q3ValueList<QByteArray>&
Set::groupNames() const
{
return d->groupNames;
}
const Q3ValueList<QByteArray>&
Set::propertyNamesForGroup(const QByteArray &group) const
{
return d->propertiesOfGroup;
return d->propertiesOfGroup[group];
}
void
Set::setGroupDescription(const QByteArray &group, const QString desc)
{
d->groupsDescription[group] = desc;
d->groupDescriptions[group] = desc;
}
QString
Set::groupDescription(const QByteArray &group)
{
if(d->groupsDescription.contains(group))
return d->groupsDescription[group];
if(d->groupDescriptions.contains(group))
return d->groupDescriptions[group];
return group;
}
void
Set::setGroupIcon(const QByteArray &group, const QString& icon)
{
d->groupIcons[group] = icon;
}
QString
Set::groupIcon(const QByteArray &group) const
{
return d->groupIcons[group];
}
/////////////////////////////////////////////////////
uint
......@@ -333,7 +365,7 @@ Set::operator= (const Set &set)
d->ownProperty = set.d->ownProperty;
d->prevSelection = set.d->prevSelection;
d->groupsDescription = set.d->groupsDescription;
d->groupDescriptions = set.d->groupDescriptions;
// Copy all properties in the list
for(Property::DictIterator it(set.d->dict); it.current(); ++it) {
......@@ -417,7 +449,7 @@ void Buffer::initialSet(const Set *set)
for(Property::DictIterator it(set->d->dict); it.current(); ++it) {
Property *prop = new Property( *it.current() );
QByteArray group = set->d->groupForProperty[it.current()];
QString groupDesc = set->d->groupsDescription[ group ];
QString groupDesc = set->d->groupDescriptions[ group ];
setGroupDescription( group, groupDesc );
addProperty( prop, group );
prop->addRelatedProperty( it.current() );
......
......@@ -35,9 +35,6 @@ namespace KoProperty {
class Property;
class SetPrivate;
typedef QMap<QByteArray, Q3ValueList<QByteArray> > StringListMap ;
typedef QMapIterator<QByteArray, QStringList> StringListMapIterator;
/*! \brief Lists holding properties in groups
\author Cedric Pasteur <cedric.pasteur@free.fr>
......@@ -143,14 +140,31 @@ class KOPROPERTY_EXPORT Set : public QObject
By default, it only calls Property::setValue(). */
void changeProperty(const QByteArray &property, const QVariant &value);
/*! Sets the i18n'ed string that will be shown in Editor to represent this group. */
/*! Sets the i18n'ed string that will be shown in Editor to represent
\a group. */
void setGroupDescription(const QByteArray &group, const QString desc);
QString groupDescription(const QByteArray &group);
/*! \return the i18n'ed description string for \a group that will
be shown in Editor to represent \a group. If there is no special
description set for the group, \a group is just returned. */
QString groupDescription(const QByteArray &group) const;
/*! Sets the icon name \a icon to be displayed for \a group. */
void setGroupIcon(const QByteArray &group, const QString& icon);
/*! \return the icons name for \a group. */
QString groupIcon(const QByteArray &group) const;
/*! \return a list of all group names. The order is the same as the order
of creation. */
const Q3ValueList<QByteArray>& groupNames() const;
const StringListMap& groups();
/*! \return a list of all property names. The order is the same as the order
of creation. */
const Q3ValueList<QByteArray>& propertyNamesForGroup(const QCString &group) const;
/*! Used by property editor to preserve previous selection when this set is assigned again. */
/*! Used by property editor to preserve previous selection when this set
is assigned again. */
QByteArray prevSelection() const;
void setPrevSelection(const QByteArray& prevSelection);
......@@ -163,6 +177,7 @@ class KOPROPERTY_EXPORT Set : public QObject
For comparing purposes, type names are case insensitive.*/
QString typeName() const;
/*! Prints debug output for this set. */
void debug();
protected:
......
......@@ -52,20 +52,24 @@ Test::Test()
setFont(f);
/* First, create the Set which will hold the properties. */
Property *p = 0;
m_set = new Set(this, "test");
m_set->setReadOnly(readOnly);
Q3CString group;
if (!flat)
QByteArray group;
if (!flat) {
group = "SimpleGroup";
m_set->setGroupDescription(group, "Simple Group");
}
m_set->addProperty(new Property("Name", "Name"), group);
(*m_set)["Name"].setAutoSync(1);
m_set->addProperty(new Property("Int", 2, "Int"), group);
m_set->addProperty(new Property("Double", 3.1415,"Double"), group);
m_set->addProperty(new Property("Bool", QVariant(true, 4), "Bool"), group);
m_set->addProperty(new Property("Date", QDate::currentDate(),"Date"), group);
m_set->addProperty(p = new Property("Date", QDate::currentDate(),"Date"), group);
p->setIcon("date");
m_set->addProperty(new Property("Time", QTime::currentTime(),"Time"), group);
m_set->addProperty(new Property("DateTime", QDateTime::currentDateTime(),"DateTime"), group);
m_set->addProperty(new Property("DateTime", QDateTime::currentDateTime(),"Date/Time"), group);
QStringList list;//keys
list << "myitem" << "otheritem" << "3rditem";
......@@ -79,23 +83,34 @@ Test::Test()
keys.append(2);
keys.append(3);
Property::ListData *listData = new Property::ListData(keys, name_list);
m_set->addProperty(new Property("List2", listData, "otheritem", "List2"), group);
m_set->addProperty(new Property("List2", listData, "otheritem", "List 2"), group);
// Complex
group = flat ? "" : "ComplexGroup";
if (!flat) {
group = "ComplexGroup";
m_set->setGroupDescription(group, "Complex Group");
}
m_set->addProperty(new Property("Rect", this->geometry(),"Rect"), group);
m_set->addProperty(new Property("Point", QPoint(3,4), "Point"), group);
m_set->addProperty(new Property("Size", QPoint(3,4), "Size"), group);
// Appearance
group = flat ? "" : "AppearanceGroup";
if (!flat) {
group = "Appearance Group";
m_set->setGroupDescription(group, "Appearance Group");
m_set->setGroupIcon(group, "appearance");
}
m_set->addProperty(new Property("Color", this->paletteBackgroundColor(),"Color"), group);
QPixmap pm(DesktopIcon("network"));
m_set->addProperty(new Property("Pixmap", pm,"Pixmap"), group);
m_set->addProperty(new Property("Font", this->font(),"Font"), group);
m_set->addProperty(p = new Property("Pixmap", pm,"Pixmap"), group);
p->setIcon("kpaint");
m_set->addProperty(p = new Property("Font", this->font(),"Font"), group);
p->setIcon("fonts");
m_set->addProperty(new Property("Cursor", QCursor(Qt::WaitCursor),"Cursor"), group);
m_set->addProperty(new Property("LineStyle", 3, "LineStyle", "", LineStyle), group);
m_set->addProperty(new Property("SizePolicy", sizePolicy(), "SizePolicy"), group);
m_set->addProperty(new Property("LineStyle", 3, "Line Style", "", LineStyle), group);
m_set->addProperty(new Property("SizePolicy", sizePolicy(), "Size Policy"), group);
// kdDebug() << m_set->groupNames() << endl;
Editor *edit = new Editor(this,true/*autosync*/);
setCentralWidget(edit);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment