return item.isNull() ? false : item.isDir();
}
+QString KFileItemModel::roleDescription(const QByteArray& role) const
+{
+ QString descr;
+
+ switch (roleIndex(role)) {
+ case NameRole: descr = i18nc("@item:intable", "Name"); break;
+ case SizeRole: descr = i18nc("@item:intable", "Size"); break;
+ case DateRole: descr = i18nc("@item:intable", "Date"); break;
+ case PermissionsRole: descr = i18nc("@item:intable", "Permissions"); break;
+ case OwnerRole: descr = i18nc("@item:intable", "Owner"); break;
+ case GroupRole: descr = i18nc("@item:intable", "Group"); break;
+ case TypeRole: descr = i18nc("@item:intable", "Type"); break;
+ case DestinationRole: descr = i18nc("@item:intable", "Destination"); break;
+ case PathRole: descr = i18nc("@item:intable", "Path"); break;
+ case NoRole: break;
+ case IsDirRole: break;
+ case IsExpandedRole: break;
+ case ExpansionLevelRole: break;
+ default: Q_ASSERT(false); break;
+ }
+
+ return descr;
+}
+
KFileItem KFileItemModel::fileItem(int index) const
{
if (index >= 0 && index < count()) {
/** @reimp */
virtual bool supportsDropping(int index) const;
+ /** @reimp */
+ virtual QString roleDescription(const QByteArray& role) const;
+
/**
* @return The file-item for the index \a index. If the index is in a valid
* range it is assured that the file-item is not null. The runtime
KItemListHeader::KItemListHeader(QGraphicsWidget* parent) :
QGraphicsWidget(parent),
- m_model(0)
+ m_model(0),
+ m_visibleRoles(),
+ m_visibleRolesWidths()
{
QStyleOptionHeader opt;
const QSize headerSize = style()->sizeFromContents(QStyle::CT_HeaderSection, &opt, QSize());
return m_model;
}
+void KItemListHeader::setVisibleRoles(const QList<QByteArray>& roles)
+{
+ m_visibleRoles = roles;
+ update();
+}
+
+QList<QByteArray> KItemListHeader::visibleRoles() const
+{
+ return m_visibleRoles;
+}
+
+void KItemListHeader::setVisibleRolesWidths(const QHash<QByteArray, qreal> rolesWidths)
+{
+ m_visibleRolesWidths = rolesWidths;
+ update();
+}
+
+QHash<QByteArray, qreal> KItemListHeader::visibleRolesWidths() const
+{
+ return m_visibleRolesWidths;
+}
+
void KItemListHeader::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
+ // Draw background
QStyleOption opt;
opt.init(widget);
opt.rect = rect().toRect();
opt.state |= QStyle::State_Horizontal;
style()->drawControl(QStyle::CE_HeaderEmptyArea, &opt, painter);
+
+ if (!m_model) {
+ return;
+ }
+
+ // Draw roles
+ // TODO: This is a rough draft only
+ QFontMetricsF fontMetrics(font());
+ QTextOption textOption(Qt::AlignLeft | Qt::AlignVCenter);
+
+ painter->setFont(font());
+ painter->setPen(palette().text().color());
+
+ const qreal margin = style()->pixelMetric(QStyle::PM_HeaderMargin);
+ qreal x = margin;
+ foreach (const QByteArray& role, m_visibleRoles) {
+ const QString roleDescription = m_model->roleDescription(role);
+ const qreal textWidth = fontMetrics.width(roleDescription);
+ QRectF rect(x, 0, textWidth, size().height());
+ painter->drawText(rect, roleDescription, textOption);
+
+ x += m_visibleRolesWidths.value(role) + margin;
+ }
}
void KItemListHeader::slotSortRoleChanged(const QByteArray& current, const QByteArray& previous)
#include <libdolphin_export.h>
#include <QGraphicsWidget>
+#include <QHash>
+#include <QList>
class KItemModelBase;
void setModel(KItemModelBase* model);
KItemModelBase* model() const;
+ void setVisibleRoles(const QList<QByteArray>& roles);
+ QList<QByteArray> visibleRoles() const;
+
+ void setVisibleRolesWidths(const QHash<QByteArray, qreal> rolesWidths);
+ QHash<QByteArray, qreal> visibleRolesWidths() const;
+
virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0);
private slots:
private:
KItemModelBase* m_model;
+ QList<QByteArray> m_visibleRoles;
+ QHash<QByteArray, qreal> m_visibleRolesWidths;
};
#endif
markVisibleRolesSizesAsDirty();
updateLayout();
+
+ if (m_header) {
+ m_header->setVisibleRoles(roles);
+ m_header->setVisibleRolesWidths(headerRolesWidths());
+ }
}
QList<QByteArray> KItemListView::visibleRoles() const
m_header = new KItemListHeader(this);
m_header->setPos(0, 0);
m_header->setModel(m_model);
+ m_header->setVisibleRoles(m_visibleRoles);
+ m_header->setVisibleRolesWidths(headerRolesWidths());
m_header->setZValue(1);
updateHeaderWidth();
m_layouter->setHeaderHeight(m_header->size().height());
if (currentBoundingRect.top() < viewGeometry.top()) {
Q_ASSERT(scrollOrientation() == Qt::Vertical);
newOffset += currentBoundingRect.top() - viewGeometry.top();
- }
- else if ((currentBoundingRect.bottom() > viewGeometry.bottom())) {
+ } else if ((currentBoundingRect.bottom() > viewGeometry.bottom())) {
Q_ASSERT(scrollOrientation() == Qt::Vertical);
newOffset += currentBoundingRect.bottom() - viewGeometry.bottom();
- }
- else if (currentBoundingRect.left() < viewGeometry.left()) {
+ } else if (currentBoundingRect.left() < viewGeometry.left()) {
if (scrollOrientation() == Qt::Horizontal) {
newOffset += currentBoundingRect.left() - viewGeometry.left();
}
- }
- else if ((currentBoundingRect.right() > viewGeometry.right())) {
+ } else if ((currentBoundingRect.right() > viewGeometry.right())) {
if (scrollOrientation() == Qt::Horizontal) {
newOffset += currentBoundingRect.right() - viewGeometry.right();
}
foreach (KItemListWidget* widget, visibleItemListWidgets()) {
widget->setVisibleRolesSizes(m_visibleRolesSizes);
}
+
+ if (m_header) {
+ m_header->setVisibleRolesWidths(headerRolesWidths());
+ }
}
if (m_layouter->itemSize().isEmpty()) {
m_header->resize(size().width(), m_header->size().height());
}
+QHash<QByteArray, qreal> KItemListView::headerRolesWidths() const
+{
+ QHash<QByteArray, qreal> rolesWidths;
+
+ QHashIterator<QByteArray, QSizeF> it(m_visibleRolesSizes);
+ while (it.hasNext()) {
+ it.next();
+ rolesWidths.insert(it.key(), it.value().width());
+ }
+
+ return rolesWidths;
+}
+
int KItemListView::calculateAutoScrollingIncrement(int pos, int range, int oldInc)
{
int inc = 0;
*/
void updateHeaderWidth();
+ /**
+ * @return The widths of each visible role that is shown in the KItemListHeader.
+ */
+ QHash<QByteArray, qreal> headerRolesWidths() const;
+
/**
* Helper function for triggerAutoScrolling().
* @param pos Logical position of the mouse relative to the range.
void setSortOrder(Qt::SortOrder order);
Qt::SortOrder sortOrder() const;
+ /**
+ * @return Translated description for the \p role. The description is e.g. used
+ * for the header in KItemListView.
+ */
virtual QString roleDescription(const QByteArray& role) const;
/**