textimportdialog.cpp 6.02 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
/**
 * \file textimportdialog.cpp
 * Dialog to import from a text (file or clipboard).
 *
 * \b Project: Kid3
 * \author Urs Fleisch
 * \date 19 Jun 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 "textimportdialog.h"
#include <QHBoxLayout>
#include <QPushButton>
#include <QFile>
#include <QClipboard>
#include <QTextStream>
#include <QApplication>
#include "config.h"
#ifdef CONFIG_USE_KDE
#include <kfiledialog.h>
#else
#include <QFileDialog>
#endif
#include "textimporter.h"
#include "importparser.h"
42
#include "configstore.h"
43
#include "contexthelp.h"
44
#include "formatlistedit.h"
45 46 47 48 49 50
#include "qtcompatmac.h"

/**
 * Constructor.
 *
 * @param parent  parent widget
51
 * @param trackDataModel track data to be filled with imported values
52 53
 */
TextImportDialog::TextImportDialog(QWidget* parent,
Urs Fleisch's avatar
Urs Fleisch committed
54 55
                                   TrackDataModel* trackDataModel) :
  QDialog(parent), m_textImporter(new TextImporter(trackDataModel))
56
{
Urs Fleisch's avatar
Urs Fleisch committed
57 58 59
  setObjectName("TextImportDialog");
  setWindowTitle(i18n("Import from File/Clipboard"));
  setSizeGripEnabled(true);
60

Urs Fleisch's avatar
Urs Fleisch committed
61 62 63
  QVBoxLayout* vboxLayout = new QVBoxLayout(this);
  vboxLayout->setSpacing(6);
  vboxLayout->setMargin(6);
64

Urs Fleisch's avatar
Urs Fleisch committed
65
  QString formatToolTip = ImportParser::getFormatToolTip();
66 67 68 69 70 71 72 73 74
  m_formatListEdit = new FormatListEdit(
        QStringList() << i18n("Format:")
                      << i18n("Header:")
                      << i18n("Tracks:"),
        QStringList() << QString()
                      << formatToolTip
                      << formatToolTip,
        this);
  vboxLayout->addWidget(m_formatListEdit);
75

Urs Fleisch's avatar
Urs Fleisch committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
  QHBoxLayout* buttonLayout = new QHBoxLayout;
  QPushButton* helpButton = new QPushButton(i18n("&Help"), this);
  helpButton->setAutoDefault(false);
  buttonLayout->addWidget(helpButton);
  connect(helpButton, SIGNAL(clicked()), this, SLOT(showHelp()));
  QPushButton* saveButton = new QPushButton(i18n("&Save Settings"), this);
  saveButton->setAutoDefault(false);
  buttonLayout->addWidget(saveButton);
  connect(saveButton, SIGNAL(clicked()), this, SLOT(saveConfig()));
  buttonLayout->addStretch();
  QPushButton* fileButton = new QPushButton(i18n("From F&ile"), this);
  fileButton->setAutoDefault(false);
  buttonLayout->addWidget(fileButton);
  connect(fileButton, SIGNAL(clicked()), this, SLOT(fromFile()));
  QPushButton* clipButton = new QPushButton(i18n("From Clip&board"), this);
  clipButton->setAutoDefault(false);
  buttonLayout->addWidget(clipButton);
  connect(clipButton, SIGNAL(clicked()), this, SLOT(fromClipboard()));
  QPushButton* closeButton = new QPushButton(i18n("&Close"), this);
  closeButton->setAutoDefault(false);
  buttonLayout->addWidget(closeButton);
  connect(closeButton, SIGNAL(clicked()), this, SLOT(accept()));
  vboxLayout->addLayout(buttonLayout);
99 100 101 102 103 104 105 106 107 108 109 110 111 112
}

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

/**
 * Clear dialog data.
 */
void TextImportDialog::clear()
{
Urs Fleisch's avatar
Urs Fleisch committed
113
  setFormatFromConfig();
114 115 116 117 118 119 120
}

/**
 * Set the format combo box and line edits from the configuration.
 */
void TextImportDialog::setFormatFromConfig()
{
121 122 123 124 125
  m_formatListEdit->setFormats(
        QList<QStringList>() << ConfigStore::s_genCfg.m_importFormatNames
                             << ConfigStore::s_genCfg.m_importFormatHeaders
                             << ConfigStore::s_genCfg.m_importFormatTracks,
        ConfigStore::s_genCfg.m_importFormatIdx);
126 127 128 129 130 131 132 133 134 135 136
}

/**
 * Import from a file.
 *
 * @param fn file name
 *
 * @return true if ok.
 */
bool TextImportDialog::importFromFile(const QString& fn)
{
Urs Fleisch's avatar
Urs Fleisch committed
137 138 139 140 141 142 143 144
  if (!fn.isEmpty()) {
    QFile file(fn);
    if (file.open(QIODevice::ReadOnly)) {
      ConfigStore::s_genCfg.m_importDir = QFileInfo(file).dir().path();
      QTextStream stream(&file);
      QString text = stream.readAll();
      if (!text.isNull() &&
          m_textImporter->updateTrackData(
145 146 147
            text,
            m_formatListEdit->getCurrentFormat(1),
            m_formatListEdit->getCurrentFormat(2))) {
Urs Fleisch's avatar
Urs Fleisch committed
148 149 150 151 152 153 154
        emit trackDataUpdated();
      }
      file.close();
      return true;
    }
  }
  return false;
155 156 157 158 159 160 161 162
}

/**
 * Let user select file, assign file contents to text and preview in
 * table.
 */
void TextImportDialog::fromFile()
{
Urs Fleisch's avatar
Urs Fleisch committed
163
  importFromFile(
164
#ifdef CONFIG_USE_KDE
Urs Fleisch's avatar
Urs Fleisch committed
165
    KFileDialog::getOpenFileName(ConfigStore::s_genCfg.m_importDir, QString::null, this)
166
#else
Urs Fleisch's avatar
Urs Fleisch committed
167
    QFileDialog::getOpenFileName(this, QString(), ConfigStore::s_genCfg.m_importDir
168
#if !defined Q_OS_WIN32 && !defined Q_OS_MAC
Urs Fleisch's avatar
Urs Fleisch committed
169
      , QString(), 0, QFileDialog::DontUseNativeDialog
170
#endif
Urs Fleisch's avatar
Urs Fleisch committed
171
      )
172
#endif
Urs Fleisch's avatar
Urs Fleisch committed
173
    );
174 175 176 177 178 179 180
}

/**
 * Assign clipboard contents to text and preview in table.
 */
void TextImportDialog::fromClipboard()
{
Urs Fleisch's avatar
Urs Fleisch committed
181 182 183 184 185 186
  QClipboard* cb = QApplication::clipboard();
  QString text = cb->text(QClipboard::Clipboard);
  if (text.isNull())
    text = cb->text(QClipboard::Selection);
  if (!text.isNull() &&
      m_textImporter->updateTrackData(
187 188 189
        text,
        m_formatListEdit->getCurrentFormat(1),
        m_formatListEdit->getCurrentFormat(2)))
Urs Fleisch's avatar
Urs Fleisch committed
190
    emit trackDataUpdated();
191 192 193 194 195 196 197
}

/**
 * Save the local settings to the configuration.
 */
void TextImportDialog::saveConfig()
{
198 199 200 201 202
  QList<QStringList> formats = m_formatListEdit->getFormats(
        &ConfigStore::s_genCfg.m_importFormatIdx);
  ConfigStore::s_genCfg.m_importFormatNames = formats.at(0);
  ConfigStore::s_genCfg.m_importFormatHeaders = formats.at(1);
  ConfigStore::s_genCfg.m_importFormatTracks = formats.at(2);
203

Urs Fleisch's avatar
Urs Fleisch committed
204
  setFormatFromConfig();
205 206 207 208 209 210 211
}

/**
 * Show help.
 */
void TextImportDialog::showHelp()
{
Urs Fleisch's avatar
Urs Fleisch committed
212
  ContextHelp::displayHelp("import-text");
213
}