File indexing completed on 2024-11-10 07:31:47

0001 /**************************************************************************
0002  *   Copyright (C) 2009-2011 Matthias Fuchs <mat69@gmx.net>                *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  *                                                                         *
0009  *   This program is distributed in the hope that it will be useful,       *
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0012  *   GNU General Public License for more details.                          *
0013  *                                                                         *
0014  *   You should have received a copy of the GNU General Public License     *
0015  *   along with this program; if not, write to the                         *
0016  *   Free Software Foundation, Inc.,                                       *
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
0018  ***************************************************************************/
0019 
0020 #include "verificationdelegate.h"
0021 #include "verificationmodel.h"
0022 #include "verifier.h"
0023 
0024 #include <KComboBox>
0025 #include <KLineEdit>
0026 
0027 struct VerificationDelegatePrivate {
0028     VerificationDelegatePrivate()
0029     {
0030     }
0031 
0032     ~VerificationDelegatePrivate()
0033     {
0034     }
0035 
0036     QStringList hashTypes;
0037 };
0038 
0039 VerificationDelegate::VerificationDelegate(QObject *parent)
0040     : QStyledItemDelegate(parent)
0041     , d(new VerificationDelegatePrivate)
0042 {
0043     d->hashTypes = Verifier::supportedVerficationTypes();
0044     d->hashTypes.sort();
0045 }
0046 
0047 QWidget *VerificationDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
0048 {
0049     Q_UNUSED(option)
0050 
0051     if (index.isValid()) {
0052         if (index.column() == VerificationModel::Type) {
0053             if (d->hashTypes.count()) {
0054                 auto *hashTypes = new KComboBox(parent);
0055                 hashTypes->addItems(d->hashTypes);
0056 
0057                 return hashTypes;
0058             }
0059         } else if (index.column() == VerificationModel::Checksum) {
0060             return new KLineEdit(parent);
0061         }
0062     }
0063 
0064     return nullptr;
0065 }
0066 
0067 void VerificationDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
0068 {
0069     if (index.isValid() && editor) {
0070         if (index.column() == VerificationModel::Type) {
0071             auto *hashTypes = static_cast<KComboBox *>(editor);
0072             const QString hashType = index.data().toString();
0073             hashTypes->setCurrentItem(hashType);
0074         } else if (index.column() == VerificationModel::Checksum) {
0075             auto *line = static_cast<KLineEdit *>(editor);
0076             const QString checksum = index.data().toString();
0077             line->setText(checksum);
0078         }
0079     }
0080 }
0081 
0082 void VerificationDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
0083 {
0084     if (index.isValid() && editor && model) {
0085         if (index.column() == VerificationModel::Type) {
0086             auto *hashTypes = static_cast<KComboBox *>(editor);
0087             model->setData(index, hashTypes->currentText());
0088         } else if (index.column() == VerificationModel::Checksum) {
0089             auto *line = static_cast<KLineEdit *>(editor);
0090             model->setData(index, line->text());
0091         }
0092     }
0093 }
0094 
0095 void VerificationDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
0096 {
0097     Q_UNUSED(index)
0098     editor->setGeometry(option.rect);
0099 }
0100 
0101 QSize VerificationDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
0102 {
0103     // make the sizeHint a little bit nicer to have more beautiful editors
0104     QSize hint;
0105     hint.setWidth(QStyledItemDelegate::sizeHint(option, index).width());
0106     hint.setHeight(option.fontMetrics.height() + 7);
0107     return hint;
0108 }
0109 
0110 #include "moc_verificationdelegate.cpp"