diff --git a/kget/conf/transfersgrouptree.cpp b/kget/conf/transfersgrouptree.cpp index d5482f7..483685c 100644 --- a/kget/conf/transfersgrouptree.cpp +++ b/kget/conf/transfersgrouptree.cpp @@ -15,49 +15,89 @@ #include "transfersgrouptree.h" #include "core/kget.h" +#include "core/transfertreemodel.h" #include "core/transfertreeselectionmodel.h" #include -TransfersGroupDelegate::TransfersGroupDelegate(QObject * parent) - : QStyledItemDelegate(parent) +#include + +TransfersGroupDelegate::TransfersGroupDelegate(QAbstractItemView *parent) + : BasicTransfersViewDelegate(parent) { +} +QWidget *TransfersGroupDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const +{ + if (index.column() == TransferTreeModel::Name) { + return new KLineEdit(parent); + } else { + return BasicTransfersViewDelegate::createEditor(parent, option, index); + } } -QWidget * TransfersGroupDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem & option, - const QModelIndex & index) const +void TransfersGroupDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { - Q_UNUSED(option) - return new GroupEditor(index, index.data(Qt::DisplayRole).toString(), parent); + if (index.column() == TransferTreeModel::Name) { + KLineEdit *groupEditor = static_cast(editor); + groupEditor->setText(index.data().toString()); + } else { + BasicTransfersViewDelegate::setEditorData(editor, index); + } } +void TransfersGroupDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const +{ + if (index.column() == TransferTreeModel::Name) { + KLineEdit *groupEditor = static_cast(editor); + const QString newName = groupEditor->text(); + const QString oldName = index.data().toString(); + + if (!newName.isEmpty()) { + foreach (const QString &groupName, KGet::transferGroupNames()) { + if (groupName == newName && groupName != oldName) { + groupEditor->setText(oldName); + return; + } + } + + KGet::renameGroup(oldName, newName); + } + } else { + BasicTransfersViewDelegate::setModelData(editor, model, index); + } +} + + TransfersGroupTree::TransfersGroupTree(QWidget *parent) : QTreeView(parent) { setItemDelegate(new TransfersGroupDelegate(this)); } -void TransfersGroupTree::commitData(QWidget *editor) +void TransfersGroupTree::setModel(QAbstractItemModel *model) { - GroupEditor * groupEditor = static_cast(editor); + QTreeView::setModel(model); - if (groupEditor->groupIndex() != currentIndex()) { - return; + int nGroups = model->rowCount(QModelIndex()); + for (int i = 0; i < nGroups; i++) { + kDebug(5001) << "openEditor for row " << i; + openPersistentEditor(model->index(i, TransferTreeModel::Status, QModelIndex())); } - const QString newName = groupEditor->text(); - if (!newName.isEmpty()){ - foreach (const QString &groupName, KGet::transferGroupNames()) { - if (groupName == newName && - groupName != model()->data(currentIndex(), Qt::DisplayRole).toString()) { - return; - } - } + setColumnWidth(0 , 250); +} - KGet::renameGroup(model()->data(currentIndex()).toString(), newName); - setCurrentIndex(QModelIndex()); +void TransfersGroupTree::rowsInserted(const QModelIndex &parent, int start, int end) +{ + if (!parent.isValid()) { + for (int i = start; i <= end; ++i) { + kDebug(5001) << "openEditor for row " << i; + openPersistentEditor(model()->index(i, TransferTreeModel::Status, parent)); + } } + + QTreeView::rowsInserted(parent, start, end); } void TransfersGroupTree::editCurrent() diff --git a/kget/conf/transfersgrouptree.h b/kget/conf/transfersgrouptree.h index f71ca56..5d6a710 100644 --- a/kget/conf/transfersgrouptree.h +++ b/kget/conf/transfersgrouptree.h @@ -14,33 +14,23 @@ #ifndef TRANSFERSGROUPTREE_H #define TRANSFERSGROUPTREE_H +#include "ui/transfersviewdelegate.h" + #include #include -#include +class GroupStatusEditor; -class GroupEditor : public KLineEdit +class TransfersGroupDelegate : public BasicTransfersViewDelegate { Q_OBJECT -public: - GroupEditor(QModelIndex group, const QString contents, QWidget * parent=0) - : KLineEdit(contents, parent), m_groupIndex(group) - { - } - - QModelIndex groupIndex() const {return m_groupIndex;} - -private: - QModelIndex m_groupIndex; -}; -class TransfersGroupDelegate : public QStyledItemDelegate -{ - Q_OBJECT -public: - TransfersGroupDelegate(QObject * parent=0); + public: + TransfersGroupDelegate(QAbstractItemView *parent); - QWidget * createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const; + 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; }; class TransfersGroupTree : public QTreeView @@ -48,6 +38,7 @@ class TransfersGroupTree : public QTreeView Q_OBJECT public: TransfersGroupTree(QWidget *parent=0); + void setModel(QAbstractItemModel *model); public slots: void editCurrent(); @@ -55,7 +46,9 @@ class TransfersGroupTree : public QTreeView void deleteSelectedGroup(); void renameSelectedGroup(); void changeIcon(const QString &icon); - void commitData(QWidget *editor); + + private: + void rowsInserted(const QModelIndex &index, int start, int end); }; #endif diff --git a/kget/conf/transfersgroupwidget.cpp b/kget/conf/transfersgroupwidget.cpp index f89f135..8b9bbea 100644 --- a/kget/conf/transfersgroupwidget.cpp +++ b/kget/conf/transfersgroupwidget.cpp @@ -27,12 +27,10 @@ TransfersGroupWidget::TransfersGroupWidget(QWidget *parent) ui.treeView->setModel(KGet::model()); ui.treeView->setSelectionModel(KGet::selectionModel()); - // hide the size, speed, percent and status columns - ui.treeView->header()->hideSection(1); - ui.treeView->header()->hideSection(2); - ui.treeView->header()->hideSection(3); - ui.treeView->header()->hideSection(4); - ui.treeView->header()->hideSection(5); + ui.treeView->header()->hideSection(TransferTreeModel::Progress); + ui.treeView->header()->hideSection(TransferTreeModel::RemainingTime); + ui.treeView->header()->hideSection(TransferTreeModel::Size); + ui.treeView->header()->hideSection(TransferTreeModel::Speed); ui.add->setGuiItem(KStandardGuiItem::add()); ui.remove->setGuiItem(KStandardGuiItem::remove()); diff --git a/kget/ui/transfersview.cpp b/kget/ui/transfersview.cpp index 44a757c..527ac2d 100644 --- a/kget/ui/transfersview.cpp +++ b/kget/ui/transfersview.cpp @@ -75,7 +75,7 @@ void TransfersView::setModel(QAbstractItemModel * model) for(int i = 0; i < nGroups; i++) { kDebug(5001) << "openEditor for row " << i; - openPersistentEditor(model->index(i, 1, QModelIndex())); + openPersistentEditor(model->index(i, TransferTreeModel::Status, QModelIndex())); } QByteArray loadedState = QByteArray::fromBase64(Settings::headerState().toAscii()); @@ -109,7 +109,7 @@ void TransfersView::rowsInserted(const QModelIndex & parent, int start, int end) for(int i = start; i <= end; i++) { kDebug(5001) << "openEditor for row " << i; - openPersistentEditor(model()->index(i, 1, parent)); + openPersistentEditor(model()->index(i, TransferTreeModel::Status, parent)); } } diff --git a/kget/ui/transfersviewdelegate.cpp b/kget/ui/transfersviewdelegate.cpp index 949f439..168b205 100644 --- a/kget/ui/transfersviewdelegate.cpp +++ b/kget/ui/transfersviewdelegate.cpp @@ -204,9 +204,8 @@ void GroupStatusButton::timerEvent(QTimerEvent *event) update(); } -GroupStatusEditor::GroupStatusEditor(const QModelIndex & index, const TransfersViewDelegate * delegate, QWidget * parent) +GroupStatusEditor::GroupStatusEditor(const QModelIndex & index, const QStyledItemDelegate * delegate, QWidget * parent) : QWidget(parent), - m_delegate(delegate), m_index(index) { setMinimumWidth(80); @@ -224,7 +223,7 @@ GroupStatusEditor::GroupStatusEditor(const QModelIndex & index, const TransfersV m_startBt->setIcon(KIcon("media-playback-start")); m_startBt->setFixedSize(36, 36); m_startBt->setIconSize(QSize(22, 22)); - m_startBt->installEventFilter(const_cast(delegate)); + m_startBt->installEventFilter(const_cast(delegate)); m_layout->addWidget(m_startBt); m_btGroup->addButton(m_startBt); @@ -234,7 +233,7 @@ GroupStatusEditor::GroupStatusEditor(const QModelIndex & index, const TransfersV m_stopBt->setIcon(KIcon("media-playback-pause")); m_stopBt->setFixedSize(36, 36); m_stopBt->setIconSize(QSize(22, 22)); - m_stopBt->installEventFilter(const_cast(delegate)); + m_stopBt->installEventFilter(const_cast(delegate)); m_layout->addWidget(m_stopBt); m_btGroup->addButton(m_stopBt); @@ -243,8 +242,7 @@ GroupStatusEditor::GroupStatusEditor(const QModelIndex & index, const TransfersV m_layout->addStretch(); m_layout->setMargin(1); - connect(m_startBt, SIGNAL(toggled(bool)), - this, SLOT(slotStatusChanged(bool))); + connect(m_startBt, SIGNAL(toggled(bool)), this, SLOT(slotStatusChanged())); } void GroupStatusEditor::setRunning(bool running) @@ -263,15 +261,61 @@ bool GroupStatusEditor::isRunning() return m_startBt->isChecked(); } -void GroupStatusEditor::slotStatusChanged(bool running) +void GroupStatusEditor::slotStatusChanged() { - Q_UNUSED(running) + emit changedStatus(this); +} + + +BasicTransfersViewDelegate::BasicTransfersViewDelegate(QAbstractItemView *parent) + : KExtendableItemDelegate(parent) +{ +} + +QWidget *BasicTransfersViewDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const +{ + if (index.column() == TransferTreeModel::Status) { + GroupStatusEditor *qroupStatusEditor = new GroupStatusEditor(index, this, parent); + connect(qroupStatusEditor, SIGNAL(changedStatus(GroupStatusEditor*)), this, SLOT(slotGroupStatusChanged(GroupStatusEditor*))); + return qroupStatusEditor; + } else { + return KExtendableItemDelegate::createEditor(parent, option, index); + } +} + +void BasicTransfersViewDelegate::slotGroupStatusChanged(GroupStatusEditor *editor) +{ + commitData(editor); +} + +void BasicTransfersViewDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const +{ + if (index.column() == TransferTreeModel::Status) { + GroupStatusEditor *groupEditor = static_cast(editor); + groupEditor->setRunning(KGet::model()->itemFromIndex(index)->asGroup()->groupHandler()->status() == JobQueue::Running); + } else { + KExtendableItemDelegate::setEditorData(editor, index); + } +} - emit const_cast(m_delegate)->commitData(this); +void BasicTransfersViewDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const +{ + if (index.column() == TransferTreeModel::Status) { + GroupStatusEditor *groupEditor = static_cast(editor); + TransferGroupHandler *groupHandler = KGet::model()->itemFromIndex(index)->asGroup()->groupHandler(); + + if (groupEditor->isRunning()) { + groupHandler->start(); + } else { + groupHandler->stop(); + } + } else { + KExtendableItemDelegate::setModelData(editor, model, index); + } } TransfersViewDelegate::TransfersViewDelegate(QAbstractItemView *parent) - : KExtendableItemDelegate(parent) + : BasicTransfersViewDelegate(parent) { Q_ASSERT(qobject_cast(parent)); setExtendPixmap(SmallIcon("arrow-right")); @@ -395,16 +439,6 @@ QSize TransfersViewDelegate::sizeHint(const QStyleOptionViewItem & option, const } } -QWidget * TransfersViewDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & option , const QModelIndex & index) const -{ - Q_UNUSED(option) - Q_UNUSED(index) - - GroupStatusEditor * groupsStatusEditor = new GroupStatusEditor(index, this, parent); - - return groupsStatusEditor; -} - bool TransfersViewDelegate::editorEvent(QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index) { Q_UNUSED(model) @@ -448,29 +482,4 @@ bool TransfersViewDelegate::editorEvent(QEvent * event, QAbstractItemModel * mod return false; } -void TransfersViewDelegate::setEditorData(QWidget * editor, const QModelIndex & index) const -{ - GroupStatusEditor * groupEditor = static_cast(editor); - - TransferTreeModel * m = KGet::model(); - - groupEditor->setRunning(m->itemFromIndex(index)->asGroup()->groupHandler()->status() == JobQueue::Running); -} - -void TransfersViewDelegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const -{ - Q_UNUSED(model) - - GroupStatusEditor * groupEditor = static_cast(editor); - - TransferTreeModel * m = KGet::model(); - - TransferGroupHandler * groupHandler = m->itemFromIndex(index)->asGroup()->groupHandler(); - - if (groupEditor->isRunning()) - groupHandler->start(); - else - groupHandler->stop(); -} - #include "transfersviewdelegate.moc" diff --git a/kget/ui/transfersviewdelegate.h b/kget/ui/transfersviewdelegate.h index 1e13c18..89548a6 100644 --- a/kget/ui/transfersviewdelegate.h +++ b/kget/ui/transfersviewdelegate.h @@ -53,17 +53,18 @@ class GroupStatusEditor : public QWidget Q_OBJECT public: - explicit GroupStatusEditor(const QModelIndex & index, const TransfersViewDelegate * delegate, QWidget * parent=0); + explicit GroupStatusEditor(const QModelIndex & index, const QStyledItemDelegate * delegate, QWidget * parent=0); void setRunning(bool running); bool isRunning(); private slots: - void slotStatusChanged(bool running); + void slotStatusChanged(); - private: - const TransfersViewDelegate * m_delegate; + signals: + void changedStatus(GroupStatusEditor *editor); + private: QModelIndex m_index; QHBoxLayout * m_layout; @@ -73,11 +74,27 @@ class GroupStatusEditor : public QWidget GroupStatusButton * m_stopBt; }; -class TransfersViewDelegate : public KExtendableItemDelegate +/** + * The BasicTransfersViewDelegate handles the setting of the status of a group + */ +class BasicTransfersViewDelegate : public KExtendableItemDelegate { Q_OBJECT - friend class GroupStatusEditor; + public: + BasicTransfersViewDelegate(QAbstractItemView *parent); + + virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; + virtual void setEditorData(QWidget *editor, const QModelIndex &index) const; + virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const; + + private slots: + virtual void slotGroupStatusChanged(GroupStatusEditor *editor); +}; + +class TransfersViewDelegate : public BasicTransfersViewDelegate +{ + Q_OBJECT public: TransfersViewDelegate(QAbstractItemView *parent); @@ -88,12 +105,7 @@ class TransfersViewDelegate : public KExtendableItemDelegate QSize sizeHint (const QStyleOptionViewItem & option, const QModelIndex & index) const; - QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem & option, const QModelIndex & index) const; - bool editorEvent(QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index); - - void setEditorData(QWidget * editor, const QModelIndex & index) const; - void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const; }; #endif