File indexing completed on 2024-10-06 04:31:43
0001 /* This file is part of the KDE project 0002 0003 Copyright (C) 2005 Dario Massarin <nekkar@libero.it> 0004 Copyright (C) 2007 Urs Wolfer <uwolfer @ kde.org> 0005 Copyright (C) 2007 Javier Goday <jgoday @ gmail.com> 0006 Copyright (C) 2009 Lukas Appelhans <l.appelhans@gmx.de> 0007 Copyright (C) 2010 Matthias Fuchs <mat69@gmx.net> 0008 0009 This program is free software; you can redistribute it and/or 0010 modify it under the terms of the GNU General Public 0011 License as published by the Free Software Foundation; either 0012 version 2 of the License, or (at your option) any later version. 0013 */ 0014 0015 #include "transfersgrouptree.h" 0016 0017 #include "core/kget.h" 0018 #include "core/transfertreemodel.h" 0019 #include "core/transfertreeselectionmodel.h" 0020 0021 #include "kget_debug.h" 0022 #include <QDebug> 0023 0024 #include <QHeaderView> 0025 0026 #include <KLineEdit> 0027 0028 TransfersGroupDelegate::TransfersGroupDelegate(QAbstractItemView *parent) 0029 : BasicTransfersViewDelegate(parent) 0030 { 0031 } 0032 0033 QWidget *TransfersGroupDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const 0034 { 0035 if (index.column() == TransferTreeModel::Name) { 0036 return new KLineEdit(parent); 0037 } else { 0038 return BasicTransfersViewDelegate::createEditor(parent, option, index); 0039 } 0040 } 0041 0042 void TransfersGroupDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const 0043 { 0044 if (index.column() == TransferTreeModel::Name) { 0045 auto *groupEditor = static_cast<KLineEdit *>(editor); 0046 groupEditor->setText(index.data().toString()); 0047 } else { 0048 BasicTransfersViewDelegate::setEditorData(editor, index); 0049 } 0050 } 0051 0052 void TransfersGroupDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const 0053 { 0054 if (index.column() == TransferTreeModel::Name) { 0055 auto *groupEditor = static_cast<KLineEdit *>(editor); 0056 const QString newName = groupEditor->text(); 0057 const QString oldName = index.data().toString(); 0058 0059 if (!newName.isEmpty()) { 0060 foreach (const QString &groupName, KGet::transferGroupNames()) { 0061 if (groupName == newName && groupName != oldName) { 0062 groupEditor->setText(oldName); 0063 return; 0064 } 0065 } 0066 0067 KGet::renameGroup(oldName, newName); 0068 } 0069 } else { 0070 BasicTransfersViewDelegate::setModelData(editor, model, index); 0071 } 0072 } 0073 0074 TransfersGroupTree::TransfersGroupTree(QWidget *parent) 0075 : QTreeView(parent) 0076 { 0077 setItemDelegate(new TransfersGroupDelegate(this)); 0078 } 0079 0080 void TransfersGroupTree::setModel(QAbstractItemModel *model) 0081 { 0082 QTreeView::setModel(model); 0083 0084 int nGroups = model->rowCount(QModelIndex()); 0085 for (int i = 0; i < nGroups; i++) { 0086 qCDebug(KGET_DEBUG) << "openEditor for row " << i; 0087 openPersistentEditor(model->index(i, TransferTreeModel::Status, QModelIndex())); 0088 } 0089 0090 setColumnWidth(0, 250); 0091 } 0092 0093 void TransfersGroupTree::rowsInserted(const QModelIndex &parent, int start, int end) 0094 { 0095 if (!parent.isValid()) { 0096 for (int i = start; i <= end; ++i) { 0097 qCDebug(KGET_DEBUG) << "openEditor for row " << i; 0098 openPersistentEditor(model()->index(i, TransferTreeModel::Status, parent)); 0099 } 0100 } 0101 0102 QTreeView::rowsInserted(parent, start, end); 0103 } 0104 0105 void TransfersGroupTree::editCurrent() 0106 { 0107 QTreeView::edit(currentIndex()); 0108 } 0109 0110 void TransfersGroupTree::addGroup() 0111 { 0112 QString groupName(i18n("New Group")); 0113 int i = 0; 0114 0115 while (KGet::transferGroupNames().contains(groupName)) { 0116 groupName = i18n("New Group") + QString::number(++i); 0117 } 0118 0119 if (KGet::addGroup(groupName)) { 0120 QModelIndex index = model()->index(model()->rowCount() - 1, 0); 0121 setCurrentIndex(index); 0122 editCurrent(); 0123 } 0124 } 0125 0126 void TransfersGroupTree::deleteSelectedGroup() 0127 { 0128 KGet::delGroups(KGet::selectedTransferGroups()); 0129 } 0130 0131 void TransfersGroupTree::renameSelectedGroup() 0132 { 0133 if (currentIndex().isValid()) 0134 editCurrent(); 0135 } 0136 0137 void TransfersGroupTree::changeIcon(const QString &icon) 0138 { 0139 qCDebug(KGET_DEBUG); 0140 TransferTreeSelectionModel *selModel = KGet::selectionModel(); 0141 0142 QModelIndexList indexList = selModel->selectedRows(); 0143 0144 if (!icon.isEmpty()) { 0145 foreach (TransferGroupHandler *group, KGet::selectedTransferGroups()) { 0146 group->setIconName(icon); 0147 } 0148 } 0149 Q_EMIT dataChanged(indexList.first(), indexList.last()); 0150 } 0151 0152 #include "moc_transfersgrouptree.cpp"