1 /* This file is part of the KDE project
2 
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>
6    Copyright (C) 2009 Lukas Appelhans <l.appelhans@gmx.de>
7    Copyright (C) 2010 Matthias Fuchs <mat69@gmx.net>
8 
9    This program is free software; you can redistribute it and/or
10    modify it under the terms of the GNU General Public
11    License as published by the Free Software Foundation; either
12    version 2 of the License, or (at your option) any later version.
13 */
14 
15 #include "transfersgrouptree.h"
16 
17 #include "core/kget.h"
18 #include "core/transfertreemodel.h"
19 #include "core/transfertreeselectionmodel.h"
20 
21 #include "kget_debug.h"
22 #include <QDebug>
23 
24 #include <QHeaderView>
25 
26 #include <KLineEdit>
27 
TransfersGroupDelegate(QAbstractItemView * parent)28 TransfersGroupDelegate::TransfersGroupDelegate(QAbstractItemView *parent)
29   : BasicTransfersViewDelegate(parent)
30 {
31 }
32 
createEditor(QWidget * parent,const QStyleOptionViewItem & option,const QModelIndex & index) const33 QWidget *TransfersGroupDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
34 {
35     if (index.column() == TransferTreeModel::Name) {
36         return new KLineEdit(parent);
37     } else {
38         return BasicTransfersViewDelegate::createEditor(parent, option, index);
39     }
40 }
41 
setEditorData(QWidget * editor,const QModelIndex & index) const42 void TransfersGroupDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
43 {
44     if (index.column() == TransferTreeModel::Name) {
45         auto *groupEditor = static_cast<KLineEdit*>(editor);
46         groupEditor->setText(index.data().toString());
47     } else {
48         BasicTransfersViewDelegate::setEditorData(editor, index);
49     }
50 }
51 
setModelData(QWidget * editor,QAbstractItemModel * model,const QModelIndex & index) const52 void TransfersGroupDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
53 {
54     if (index.column() == TransferTreeModel::Name) {
55         auto *groupEditor = static_cast<KLineEdit*>(editor);
56         const QString newName = groupEditor->text();
57         const QString oldName = index.data().toString();
58 
59         if (!newName.isEmpty()) {
60             foreach (const QString &groupName, KGet::transferGroupNames()) {
61                 if (groupName == newName && groupName != oldName) {
62                     groupEditor->setText(oldName);
63                     return;
64                 }
65             }
66 
67             KGet::renameGroup(oldName, newName);
68         }
69     } else {
70         BasicTransfersViewDelegate::setModelData(editor, model, index);
71     }
72 }
73 
74 
TransfersGroupTree(QWidget * parent)75 TransfersGroupTree::TransfersGroupTree(QWidget *parent)
76     : QTreeView(parent)
77 {
78     setItemDelegate(new TransfersGroupDelegate(this));
79 }
80 
setModel(QAbstractItemModel * model)81 void TransfersGroupTree::setModel(QAbstractItemModel *model)
82 {
83     QTreeView::setModel(model);
84 
85     int nGroups = model->rowCount(QModelIndex());
86     for (int i = 0; i < nGroups; i++) {
87         qCDebug(KGET_DEBUG) << "openEditor for row " << i;
88         openPersistentEditor(model->index(i, TransferTreeModel::Status, QModelIndex()));
89     }
90 
91     setColumnWidth(0 , 250);
92 }
93 
rowsInserted(const QModelIndex & parent,int start,int end)94 void TransfersGroupTree::rowsInserted(const QModelIndex &parent, int start, int end)
95 {
96     if (!parent.isValid()) {
97         for (int i = start; i <= end; ++i) {
98             qCDebug(KGET_DEBUG) << "openEditor for row " << i;
99             openPersistentEditor(model()->index(i, TransferTreeModel::Status, parent));
100         }
101     }
102 
103     QTreeView::rowsInserted(parent, start, end);
104 }
105 
editCurrent()106 void TransfersGroupTree::editCurrent()
107 {
108     QTreeView::edit(currentIndex());
109 }
110 
addGroup()111 void TransfersGroupTree::addGroup()
112 {
113     QString groupName(i18n("New Group"));
114     int i=0;
115 
116     while(KGet::transferGroupNames().contains(groupName))
117     {
118         groupName = i18n("New Group") + QString::number(++i);
119     }
120 
121     if (KGet::addGroup(groupName)) {
122         QModelIndex index = model()->index(model()->rowCount() - 1, 0);
123         setCurrentIndex(index);
124         editCurrent();
125     }
126 }
127 
deleteSelectedGroup()128 void TransfersGroupTree::deleteSelectedGroup()
129 {
130     KGet::delGroups(KGet::selectedTransferGroups());
131 }
132 
renameSelectedGroup()133 void TransfersGroupTree::renameSelectedGroup()
134 {
135     if(currentIndex().isValid())
136         editCurrent();
137 }
138 
changeIcon(const QString & icon)139 void TransfersGroupTree::changeIcon(const QString &icon)
140 {
141     qCDebug(KGET_DEBUG);
142     TransferTreeSelectionModel *selModel = KGet::selectionModel();
143 
144     QModelIndexList indexList = selModel->selectedRows();
145 
146     if (!icon.isEmpty())
147     {
148         foreach (TransferGroupHandler *group, KGet::selectedTransferGroups())
149         {
150             group->setIconName(icon);
151         }
152     }
153     Q_EMIT dataChanged(indexList.first(),indexList.last());
154 }
155