File indexing completed on 2025-02-23 04:34:24

0001 /**
0002  * \file stringlistedit.cpp
0003  * Widget to edit a string list.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 14 Apr 2007
0008  *
0009  * Copyright (C) 2007-2024  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #include "stringlistedit.h"
0028 #include <QLineEdit>
0029 #include <QInputDialog>
0030 #include <QLayout>
0031 #include <QListView>
0032 
0033 /**
0034  * Constructor.
0035  *
0036  * @param model item model, e.g. a QStringListModel
0037  * @param parent parent widget
0038  */
0039 StringListEdit::StringListEdit(QAbstractItemModel* model, QWidget* parent)
0040   : AbstractListEdit(new QListView, model, parent)
0041 {
0042   setObjectName(QLatin1String("StringListEdit"));
0043 }
0044 
0045 /**
0046  * Add a new item.
0047  */
0048 void StringListEdit::addItem()
0049 {
0050   bool ok;
0051   if (QString txt = QInputDialog::getText(
0052         this, tr("Add Item"), QString(), QLineEdit::Normal,
0053         QString(), &ok);
0054       ok && !txt.isEmpty()) {
0055     QAbstractItemModel* model = getItemView()->model();
0056     int row = model->rowCount();
0057     model->insertRow(row);
0058     model->setData(model->index(row, 0), txt);
0059   }
0060 }
0061 
0062 /**
0063  * Edit the selected item.
0064  */
0065 void StringListEdit::editItem()
0066 {
0067   if (QModelIndex index = getItemView()->currentIndex(); index.isValid()) {
0068     QAbstractItemModel* model = getItemView()->model();
0069     bool ok;
0070     if (QString txt = QInputDialog::getText(
0071           this, tr("Edit Item"), QString(), QLineEdit::Normal,
0072           model->data(index, Qt::EditRole).toString(), &ok);
0073         ok && !txt.isEmpty()) {
0074       model->setData(index, txt);
0075     }
0076   }
0077 }