File indexing completed on 2024-05-12 05:09:46

0001 /***************************************************************************
0002     Copyright (C) 2005-2009 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #include "choicefieldwidget.h"
0026 #include "../field.h"
0027 
0028 #include <QComboBox>
0029 #include <QGuiApplication>
0030 #include <QScreen>
0031 
0032 namespace {
0033   const double MAX_FRACTION_SCREEN_WIDTH = 0.4;
0034 }
0035 
0036 using Tellico::GUI::ChoiceFieldWidget;
0037 
0038 ChoiceFieldWidget::ChoiceFieldWidget(Tellico::Data::FieldPtr field_, QWidget* parent_)
0039     : FieldWidget(field_, parent_), m_comboBox(nullptr) {
0040 
0041   m_comboBox = new QComboBox(this);
0042   void (QComboBox::* indexChanged)(int) = &QComboBox::currentIndexChanged;
0043   connect(m_comboBox, indexChanged, this, &ChoiceFieldWidget::checkModified);
0044   m_maxTextWidth = MAX_FRACTION_SCREEN_WIDTH * QGuiApplication::primaryScreen()->size().width();
0045 
0046   QStringList values = field_->allowed();
0047   // always have empty choice, but don't show two empty values
0048   values.removeAll(QString());
0049 
0050   const QFontMetrics& fm = fontMetrics();
0051   m_comboBox->addItem(QString(), QString());
0052   foreach(const QString& value, values) {
0053     m_comboBox->addItem(fm.elidedText(value, Qt::ElideMiddle, m_maxTextWidth), value);
0054   }
0055 
0056   m_comboBox->setMinimumWidth(5*fm.maxWidth());
0057   registerWidget();
0058 }
0059 
0060 QString ChoiceFieldWidget::text() const {
0061   return m_comboBox->currentData().toString();
0062 }
0063 
0064 void ChoiceFieldWidget::setTextImpl(const QString& text_) {
0065   int idx = m_comboBox->findData(text_);
0066   if(idx < 0) {
0067     m_comboBox->addItem(fontMetrics().elidedText(text_, Qt::ElideMiddle, m_maxTextWidth), text_);
0068     m_comboBox->setCurrentIndex(m_comboBox->count()-1);
0069   } else {
0070     m_comboBox->setCurrentIndex(idx);
0071   }
0072 }
0073 
0074 void ChoiceFieldWidget::clearImpl() {
0075   m_comboBox->setCurrentIndex(0); // first item is empty
0076   editMultiple(false);
0077 }
0078 
0079 void ChoiceFieldWidget::updateFieldHook(Tellico::Data::FieldPtr, Tellico::Data::FieldPtr newField_) {
0080   const QString oldValue = text();
0081   int idx = m_comboBox->currentIndex();
0082   m_comboBox->blockSignals(true);
0083   m_comboBox->clear();
0084 
0085   QStringList values = newField_->allowed();
0086   values.removeAll(QString());
0087 
0088   const QFontMetrics& fm = fontMetrics();
0089   // always have empty choice
0090   m_comboBox->addItem(QString(), QString());
0091   foreach(const QString& value, values) {
0092     m_comboBox->addItem(fm.elidedText(value, Qt::ElideMiddle, m_maxTextWidth), value);
0093   }
0094   m_comboBox->setCurrentIndex(idx);
0095   m_comboBox->blockSignals(false);
0096   setTextImpl(oldValue); // set back to previous value
0097 }
0098 
0099 QWidget* ChoiceFieldWidget::widget() {
0100   return m_comboBox;
0101 }