formatlistedit.cpp 7.62 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/**
 * \file formatlistedit.cpp
 * Widget to edit a format list.
 *
 * \b Project: Kid3
 * \author Urs Fleisch
 * \date 13 Aug 2011
 *
 * Copyright (C) 2011  Urs Fleisch
 *
 * This file is part of Kid3.
 *
 * Kid3 is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Kid3 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "formatlistedit.h"
#include <QPushButton>
#include <QLineEdit>
#include <QComboBox>
#include <QLayout>
#include <QFormLayout>
#include <QSizePolicy>
#include "qtcompatmac.h"

/**
 * Constructor.
 *
 * @param labels list of label texts for fields in a single format
 * @param tooltips list of tooltips, one string per label, empty if not used
 * @param parent parent widget
 */
FormatListEdit::FormatListEdit(const QStringList& labels,
                               const QStringList& tooltips,
                               QWidget* parent) :
  QWidget(parent)
{
  setObjectName("FormatListEdit");
  QHBoxLayout* hlayout = new QHBoxLayout(this);
  hlayout->setMargin(0);
  QFormLayout* formatLayout = new QFormLayout;
52
  formatLayout->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  bool comboBoxCreated = false;
  for (int i = 0; i < labels.size(); ++i) {
    const QString& label = labels.at(i);
    const QString& toolTip = tooltips.at(i);
    if (!comboBoxCreated) {
      m_formatComboBox = new QComboBox;
      m_formatComboBox->setEditable(true);
      m_formatComboBox->setInsertPolicy(QComboBox::NoInsert);
      connect(m_formatComboBox, SIGNAL(activated(int)),
              this, SLOT(updateLineEdits(int)));
      connect(m_formatComboBox->lineEdit(), SIGNAL(editingFinished()),
              this, SLOT(commitCurrentEdits()));
      if (!toolTip.isEmpty())
        m_formatComboBox->setToolTip(toolTip);
      formatLayout->addRow(label, m_formatComboBox);
      comboBoxCreated = true;
    } else {
      QLineEdit* ed = new QLineEdit;
      connect(ed, SIGNAL(returnPressed()), this, SIGNAL(formatChanged()));
      if (!toolTip.isEmpty())
        ed->setToolTip(toolTip);
      formatLayout->addRow(label, ed);
      m_lineEdits.append(ed);
    }
  }
  hlayout->addLayout(formatLayout);
  QVBoxLayout* vlayout = new QVBoxLayout;
80
  vlayout->setSpacing(2);
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
  m_addPushButton = new QPushButton(i18n("&Add"));
  m_addPushButton->setAutoDefault(false);
  m_removePushButton = new QPushButton(i18n("&Remove"));
  m_removePushButton->setAutoDefault(false);
  vlayout->addWidget(m_addPushButton);
  vlayout->addWidget(m_removePushButton);
  vlayout->addStretch();
  hlayout->addLayout(vlayout);
  connect(m_addPushButton, SIGNAL(clicked()), this, SLOT(addItem()));
  connect(m_removePushButton, SIGNAL(clicked()), this, SLOT(removeItem()));
  setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
}

/**
 * Destructor.
 */
FormatListEdit::~FormatListEdit()
{
}

/**
 * Set format strings.
 *
 * @param formats list of format stringlists, the first stringlist contains
 *   the names, the second the corresponding string for the first line edit,
 *   etc.
 * @param index index to select, -1 to keep current index
 */
void FormatListEdit::setFormats(const QList<QStringList>& formats, int index)
{
  m_formats = formats;
  if (index >= 0) {
    m_formatComboBox->setCurrentIndex(index);
    updateComboBoxAndLineEdits(index);
  }
}

/**
 * Get format strings.
 *
 * @param index  if not null, the current index is returned here
 *
 * @return list of format stringlists, the first stringlist contains
 *   the names, the second the corresponding string for the first line edit,
 *   etc.
 */
QList<QStringList> FormatListEdit::getFormats(int* index)
{
  commitCurrentEdits();
  if (index) {
    *index = m_formatComboBox->currentIndex();
  }
  return m_formats;
}

/**
 * Get a format string from the format currently displayed in the GUI.
 *
 * @param formatNr index of the format stringlist, 0 is the format name
 *   1 the first line edit, etc.
 *
 * @return format string.
 */
QString FormatListEdit::getCurrentFormat(int formatNr) const
{
  if (formatNr == 0) {
    return m_formatComboBox->currentText();
  } else if (formatNr > 0 && formatNr - 1 < m_lineEdits.size()) {
    return m_lineEdits.at(formatNr - 1)->text();
  } else {
    return QString();
  }
}

/**
 * Update GUI controls from formats.
 *
 * @param index combo box index to set
 */
void FormatListEdit::updateComboBoxAndLineEdits(int index)
{
  m_formatComboBox->clear();
  if (!m_formats.isEmpty()) {
    m_formatComboBox->addItems(m_formats.first());
    if (index >= 0 && index < m_formats.first().size()) {
      m_formatComboBox->setCurrentIndex(index);
      updateLineEdits(index);
    }
  }
}

/**
 * Set the currently selected format from the contents of the controls.
 */
void FormatListEdit::commitCurrentEdits()
{
  int index = m_formatComboBox->currentIndex();
  if (index < 0)
    return;

  if (m_formatComboBox->itemText(index) != m_formatComboBox->currentText()) {
    m_formatComboBox->setItemText(index,  m_formatComboBox->currentText());
  }

  for (int i = 0; i < m_formats.size() && i - 1 < m_lineEdits.size(); ++i) {
    QString text(i == 0
                 ? m_formatComboBox->currentText()
                 : m_lineEdits.at(i - 1)->text());
    QStringList& fmts = m_formats[i];
    if (index < fmts.size()) {
      fmts[index] = text;
    }
  }
}

/**
 * Set the format lineedits to the format of the index.
 *
 * @param index selected item in combo box
 */
void FormatListEdit::updateLineEdits(int index)
{
  for (int i = 0; i < m_lineEdits.size() && i + 1 < m_formats.size(); ++i) {
    QLineEdit* le = m_lineEdits.at(i);
    const QStringList& fmts = m_formats.at(i + 1);
    if (index < fmts.size()) {
      le->setText(fmts.at(index));
    } else {
      le->clear();
    }
  }
  emit formatChanged();
}

/**
 * Add a new item.
 */
void FormatListEdit::addItem()
{
  commitCurrentEdits();
  if (!m_formats.isEmpty()) {
    // first search for an existing empty format
    int index = -1;
    for (int fmtIdx = m_formats.first().size() - 1; fmtIdx > 0; --fmtIdx) {
      bool allEmpty = true;
      for (int leIdx = 1; leIdx < m_formats.size(); ++leIdx) {
        const QStringList& fmts = m_formats.at(leIdx);
        if (fmtIdx < fmts.size() && !fmts.at(fmtIdx).isEmpty()) {
          allEmpty = false;
          break;
        }
      }
      if (allEmpty) {
        index = fmtIdx;
        break;
      }
    }

    if (index == -1) {
      // no empty format found, add a new one
      for (int i = 0; i < m_formats.size(); ++i) {
        m_formats[i].append(i == 0 ? i18n("New") : "");
      }
      index = m_formats.first().size() - 1;
    }
    updateComboBoxAndLineEdits(index);
    m_formatComboBox->lineEdit()->setFocus();
    m_formatComboBox->lineEdit()->selectAll();
  }
}

/**
 * Remove the selected item.
 */
void FormatListEdit::removeItem()
{
  int index = m_formatComboBox->currentIndex();
  if (index < 0)
    return;

  for (int i = 0; i < m_formats.size(); ++i) {
    if (index < m_formats[i].size()) {
      m_formats[i].removeAt(index);
    }
  }
  if (!m_formats.isEmpty()) {
    const QStringList& fmts = m_formats.first();
    if (index >= fmts.size()) {
      index = fmts.size() - 1;
    }
    if (index < 0) {
      addItem();
    } else {
      updateComboBoxAndLineEdits(index);
    }
  }
}