1 /* This file is part of the KDE project
3 Copyright (C) 2005 Dario Massarin <nekkar@libero.it>
4 Copyright (C) 2007 Urs Wolfer <uwolfer @ kde.org>
5 Copyright (C) 2007 Javier Goday <jgoday @ gmail.com>
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
13 #include "transfersgroupwidget.h"
15 #include "core/kget.h"
16 #include "core/transfergrouphandler.h"
17 #include "core/transfertreeselectionmodel.h"
19 #include <KMessageBox>
20 #include <KIconButton>
23 #include <QHBoxLayout>
24 #include <QPushButton>
25 #include <QHeaderView>
29 TransfersGroupDelegate::TransfersGroupDelegate(QObject
* parent
)
30 : QStyledItemDelegate(parent
)
35 QWidget
* TransfersGroupDelegate::createEditor(QWidget
* parent
, const QStyleOptionViewItem
& option
,
36 const QModelIndex
& index
) const
39 return new GroupEditor(index
, index
.model()->data(index
, Qt::DisplayRole
).toString(), parent
);
42 TransfersGroupTree::TransfersGroupTree(QWidget
*parent
)
45 setItemDelegate(new TransfersGroupDelegate(this));
47 setRootIsDecorated(false);
48 setAlternatingRowColors(true);
49 setSelectionMode(QAbstractItemView::SingleSelection
);
51 KGet::addTransferView(this);
53 // hide the size, speed, percent and status columns
54 header()->hideSection(1);
55 header()->hideSection(2);
56 header()->hideSection(3);
57 header()->hideSection(4);
58 header()->hideSection(5);
60 setItemsExpandable(false);
61 setSelectionModel((QItemSelectionModel
*) KGet::selectionModel());
64 void TransfersGroupTree::commitData(QWidget
*editor
)
66 GroupEditor
* groupEditor
= static_cast<GroupEditor
*>(editor
);
68 if(groupEditor
->groupIndex() != currentIndex())
71 if (groupEditor
->text().isEmpty())
73 KMessageBox::error( this, i18n("The group name is empty"), i18n("A group can not have an empty name\nPlease select a new one") );
74 QTimer::singleShot( 0, this, SLOT(editCurrent()) );
79 foreach(const QString
&groupName
, KGet::transferGroupNames())
81 if(groupName
== groupEditor
->text() &&
82 groupName
!= ((TransferGroupHandler
*) currentIndex().internalPointer())->name() )
84 KMessageBox::error( this, i18n("Group name already in use"), i18n("Another group with this name already exists\nPlease select a different name") );
85 QTimer::singleShot( 0, this, SLOT(editCurrent()) );
90 KGet::renameGroup(model()->data(currentIndex()).toString(), groupEditor
->text());
91 setCurrentIndex(QModelIndex());
95 void TransfersGroupTree::editCurrent()
97 QTreeView::edit(currentIndex());
100 void TransfersGroupTree::addGroup()
102 QString
groupName(i18n("New Group"));
105 while(KGet::transferGroupNames().contains(groupName
))
107 groupName
= i18n("New Group") + QString::number(++i
);
110 if (KGet::addGroup(groupName
)) {
111 QModelIndex index
= model()->index(model()->rowCount() - 1, 0);
112 setCurrentIndex(index
);
117 void TransfersGroupTree::deleteSelectedGroup()
119 QItemSelectionModel
*selModel
= selectionModel();
120 QAbstractItemModel
*dataModel
= model();
122 QModelIndexList indexList
= selModel
->selectedRows();
124 foreach(const QModelIndex
&index
, indexList
)
126 QString groupName
= dataModel
->data(index
, Qt::DisplayRole
).toString();
128 if(groupName
== i18n("My Downloads"))
130 KMessageBox::sorry(this, i18n("You can not delete this group!"));
134 if(KMessageBox::questionYesNoCancel(this,
135 i18n("Are you sure that you want to remove\n"
136 "the group named %1?", groupName
)) == KMessageBox::Yes
)
137 KGet::delGroup(groupName
);
141 void TransfersGroupTree::renameSelectedGroup()
143 if(currentIndex().isValid())
147 void TransfersGroupTree::changeIcon(const QString
&icon
)
150 TransferTreeSelectionModel
*selModel
= KGet::selectionModel();
152 QModelIndexList indexList
= selModel
->selectedRows();
156 foreach (TransferGroupHandler
*group
, KGet::selectedTransferGroups())
158 group
->setIconName(icon
);
161 emit
dataChanged(indexList
.first(),indexList
.last());
165 TransfersGroupWidget::TransfersGroupWidget(QWidget
*parent
)
168 m_view
= new TransfersGroupTree(parent
);
170 addButton
= new QPushButton(i18n("Add"));
171 addButton
->setIcon(KIcon("list-add"));
172 deleteButton
= new QPushButton(i18n("Delete"));
173 deleteButton
->setIcon(KIcon("list-remove"));
174 deleteButton
->setEnabled(false);
175 renameButton
= new QPushButton(i18n("Rename"));
176 renameButton
->setIcon(KIcon("edit-rename"));
177 renameButton
->setEnabled(false);
178 iconButton
= new KIconButton(dynamic_cast<QWidget
*>(this));
179 iconButton
->setIconSize(32);
180 iconButton
->setButtonIconSize(16);
181 iconButton
->setText(i18n("Select Icon"));
182 iconButton
->setIcon(KIcon("preferences-desktop-icons"));
183 configureButton
= new QPushButton(i18n("Configure..."));
184 configureButton
->setIcon(KGet::actionCollection()->action("transfer_group_settings")->icon());
186 QHBoxLayout
*buttonsLayout
= new QHBoxLayout();
187 buttonsLayout
->addWidget(addButton
);
188 buttonsLayout
->addWidget(renameButton
);
189 buttonsLayout
->addWidget(deleteButton
);
190 buttonsLayout
->addWidget(iconButton
);
191 buttonsLayout
->addWidget(configureButton
);
194 addLayout(buttonsLayout
);
196 connect(addButton
, SIGNAL(clicked()), m_view
, SLOT(addGroup()));
197 connect(deleteButton
, SIGNAL(clicked()), m_view
, SLOT(deleteSelectedGroup()));
198 connect(renameButton
, SIGNAL(clicked()), m_view
, SLOT(renameSelectedGroup()));
199 connect(iconButton
, SIGNAL(iconChanged(const QString
&)), m_view
, SLOT(changeIcon(const QString
&)));
200 connect(configureButton
, SIGNAL(clicked()), KGet::actionCollection()->action("transfer_group_settings"), SLOT(trigger()));
201 connect(m_view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
202 this, SLOT(slotSelectionChanged(const QItemSelection
&, const QItemSelection
&)));
205 void TransfersGroupWidget::slotSelectionChanged(const QItemSelection
&newSelection
,
206 const QItemSelection
&oldSelection
)
208 Q_UNUSED(oldSelection
)
210 bool canDelete
= true;
212 foreach(const QModelIndex
&index
, newSelection
.indexes()) {
213 if(index
.row() == 0) {
218 renameButton
->setEnabled(canDelete
);
219 deleteButton
->setEnabled(canDelete
);