diff --git a/src/kdefrontend/spreadsheet/ExportSpreadsheetDialog.cpp b/src/kdefrontend/spreadsheet/ExportSpreadsheetDialog.cpp index 10bdeddf1..8adf05a0a 100644 --- a/src/kdefrontend/spreadsheet/ExportSpreadsheetDialog.cpp +++ b/src/kdefrontend/spreadsheet/ExportSpreadsheetDialog.cpp @@ -1,523 +1,535 @@ /*************************************************************************** File : ExportSpreadsheetDialog.cpp Project : LabPlot Description : export spreadsheet dialog -------------------------------------------------------------------- Copyright : (C) 2014-2019 by Alexander Semke (alexander.semke@web.de) ***************************************************************************/ /*************************************************************************** * * * This program 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. * * * * This program 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, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * ***************************************************************************/ #include "ExportSpreadsheetDialog.h" #include "ui_exportspreadsheetwidget.h" #include "backend/datasources/filters/AbstractFileFilter.h" #include "backend/datasources/filters/AsciiFilter.h" #include #include #include #include #include #include #include #include #include #include #include #include /*! \class ExportSpreadsheetDialog \brief Dialog for exporting a spreadsheet to a file. \ingroup kdefrontend */ ExportSpreadsheetDialog::ExportSpreadsheetDialog(QWidget* parent) : QDialog(parent), ui(new Ui::ExportSpreadsheetWidget()) { ui->setupUi(this); ui->gbOptions->hide(); QDialogButtonBox* btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); m_showOptionsButton = new QPushButton; connect(btnBox, &QDialogButtonBox::clicked, this, &ExportSpreadsheetDialog::slotButtonClicked); btnBox->addButton(m_showOptionsButton, QDialogButtonBox::ActionRole); ui->verticalLayout->addWidget(btnBox); m_okButton = btnBox->button(QDialogButtonBox::Ok); m_cancelButton = btnBox->button(QDialogButtonBox::Cancel); ui->leFileName->setCompleter(new QCompleter(new QDirModel, this)); ui->cbFormat->addItem("ASCII", ASCII); ui->cbFormat->addItem("Binary", Binary); ui->cbFormat->addItem("LaTeX", LaTeX); ui->cbFormat->addItem("FITS", FITS); const QStringList& drivers = QSqlDatabase::drivers(); if (drivers.contains(QLatin1String("QSQLITE")) || drivers.contains(QLatin1String("QSQLITE3"))) ui->cbFormat->addItem("SQLite", SQLite); QStringList separators = AsciiFilter::separatorCharacters(); separators.takeAt(0); //remove the first entry "auto" ui->cbSeparator->addItems(separators); ui->cbNumberFormat->addItems(AbstractFileFilter::numberFormats()); ui->cbLaTeXExport->addItem(i18n("Export Spreadsheet")); ui->cbLaTeXExport->addItem(i18n("Export Selection")); ui->bOpen->setIcon( QIcon::fromTheme("document-open") ); ui->leFileName->setFocus(); const QString textNumberFormatShort = i18n("This option determines how the convert numbers to strings."); ui->lNumberFormat->setToolTip(textNumberFormatShort); ui->cbNumberFormat->setToolTip(textNumberFormatShort); - connect(btnBox, &QDialogButtonBox::accepted, this, &ExportSpreadsheetDialog::accept); - connect(btnBox, &QDialogButtonBox::rejected, this, &ExportSpreadsheetDialog::reject); - connect(ui->bOpen, &QPushButton::clicked, this, &ExportSpreadsheetDialog::selectFile); connect(ui->leFileName, &QLineEdit::textChanged, this, &ExportSpreadsheetDialog::fileNameChanged ); connect(m_showOptionsButton, &QPushButton::clicked, this, &ExportSpreadsheetDialog::toggleOptions); connect(ui->cbFormat, static_cast(&QComboBox::currentIndexChanged), this, &ExportSpreadsheetDialog::formatChanged); connect(ui->cbExportToFITS, static_cast(&QComboBox::currentIndexChanged), this, &ExportSpreadsheetDialog::fitsExportToChanged); setWindowTitle(i18nc("@title:window", "Export Spreadsheet")); setWindowIcon(QIcon::fromTheme("document-export-database")); //restore saved settings if available KConfigGroup conf(KSharedConfig::openConfig(), "ExportSpreadsheetDialog"); KWindowConfig::restoreWindowSize(windowHandle(), conf); ui->cbFormat->setCurrentIndex(conf.readEntry("Format", 0)); ui->chkExportHeader->setChecked(conf.readEntry("Header", true)); ui->cbSeparator->setCurrentItem(conf.readEntry("Separator", "TAB")); ui->cbNumberFormat->setCurrentIndex(conf.readEntry("NumberFormat", (int)QLocale::AnyLanguage)); ui->chkHeaders->setChecked(conf.readEntry("LaTeXHeaders", true)); ui->chkGridLines->setChecked(conf.readEntry("LaTeXGridLines", true)); ui->chkCaptions->setChecked(conf.readEntry("LaTeXCaptions", true)); ui->chkEmptyRows->setChecked(conf.readEntry("LaTeXSkipEmpty", false)); ui->cbLaTeXExport->setCurrentIndex(conf.readEntry("ExportOnly", 0)); ui->chkMatrixHHeader->setChecked(conf.readEntry("MatrixHorizontalHeader", true)); ui->chkMatrixVHeader->setChecked(conf.readEntry("MatrixVerticalHeader", true)); ui->chkMatrixVHeader->setChecked(conf.readEntry("FITSSpreadsheetColumnsUnits", true)); ui->cbExportToFITS->setCurrentIndex(conf.readEntry("FITSTo", 0)); m_showOptions = conf.readEntry("ShowOptions", false); ui->gbOptions->setVisible(m_showOptions); m_showOptions ? m_showOptionsButton->setText(i18n("Hide Options")) : m_showOptionsButton->setText(i18n("Show Options")); create(); // ensure there's a window created if (conf.exists()) { KWindowConfig::restoreWindowSize(windowHandle(), conf); resize(windowHandle()->size()); // workaround for QTBUG-40584 } else resize(QSize(0, 0).expandedTo(minimumSize())); } ExportSpreadsheetDialog::~ExportSpreadsheetDialog() { //save current settings KConfigGroup conf(KSharedConfig::openConfig(), "ExportSpreadsheetDialog"); conf.writeEntry("Format", ui->cbFormat->currentIndex()); conf.writeEntry("Header", ui->chkExportHeader->isChecked()); conf.writeEntry("Separator", ui->cbSeparator->currentText()); conf.writeEntry("NumberFormat", ui->cbNumberFormat->currentIndex()); conf.writeEntry("ShowOptions", m_showOptions); conf.writeEntry("LaTeXHeaders", ui->chkHeaders->isChecked()); conf.writeEntry("LaTeXGridLines", ui->chkGridLines->isChecked()); conf.writeEntry("LaTeXCaptions", ui->chkCaptions->isChecked()); conf.writeEntry("LaTeXSkipEmpty", ui->chkEmptyRows->isChecked()); conf.writeEntry("ExportOnly", ui->cbLaTeXExport->currentIndex()); conf.writeEntry("MatrixVerticalHeader", ui->chkMatrixVHeader->isChecked()); conf.writeEntry("MatrixHorizontalHeader", ui->chkMatrixHHeader->isChecked()); conf.writeEntry("FITSTo", ui->cbExportToFITS->currentIndex()); conf.writeEntry("FITSSpreadsheetColumnsUnits", ui->chkColumnsAsUnits->isChecked()); KWindowConfig::saveWindowSize(windowHandle(), conf); } void ExportSpreadsheetDialog::setFileName(const QString& name) { KConfigGroup conf(KSharedConfig::openConfig(), "ExportSpreadsheetDialog"); QString dir = conf.readEntry("LastDir", ""); if (dir.isEmpty()) dir = QDir::homePath(); ui->leFileName->setText(dir + QDir::separator() + name); this->formatChanged(ui->cbFormat->currentIndex()); } void ExportSpreadsheetDialog::fitsExportToChanged(int idx) { if (idx == 0) { ui->chkColumnsAsUnits->hide(); ui->lColumnAsUnits->hide(); } else { if (!m_matrixMode) { ui->chkColumnsAsUnits->show(); ui->lColumnAsUnits->show(); } } } void ExportSpreadsheetDialog::setMatrixMode(bool b) { if (b) { setWindowTitle(i18nc("@title:window", "Export Matrix")); ui->lExportHeader->hide(); ui->chkExportHeader->hide(); ui->lEmptyRows->hide(); ui->chkEmptyRows->hide(); if (ui->cbFormat->currentIndex() != 3) { ui->chkMatrixHHeader->show(); ui->chkMatrixVHeader->show(); ui->lMatrixHHeader->show(); ui->lMatrixVHeader->show(); } ui->lHeader->hide(); ui->chkHeaders->hide(); ui->cbLaTeXExport->setItemText(0,i18n("Export matrix")); ui->cbExportToFITS->setCurrentIndex(0); ui->lColumnAsUnits->hide(); ui->chkColumnsAsUnits->hide(); m_matrixMode = b; } } QString ExportSpreadsheetDialog::path() const { return ui->leFileName->text(); } int ExportSpreadsheetDialog::exportToFits() const { return ui->cbExportToFITS->currentIndex(); } bool ExportSpreadsheetDialog::exportHeader() const { return ui->chkExportHeader->isChecked(); } bool ExportSpreadsheetDialog::captions() const { return ui->chkCaptions->isChecked(); } bool ExportSpreadsheetDialog::exportLatexHeader() const { return ui->chkHeaders->isChecked(); } bool ExportSpreadsheetDialog::gridLines() const { return ui->chkGridLines->isChecked(); } bool ExportSpreadsheetDialog::skipEmptyRows() const { return ui->chkEmptyRows->isChecked(); } bool ExportSpreadsheetDialog::exportSelection() const { return ui->cbLaTeXExport->currentIndex() == 1; } bool ExportSpreadsheetDialog::entireSpreadheet() const { return ui->cbLaTeXExport->currentIndex() == 0; } bool ExportSpreadsheetDialog::matrixHorizontalHeader() const { return ui->chkMatrixHHeader->isChecked(); } bool ExportSpreadsheetDialog::matrixVerticalHeader() const { return ui->chkMatrixVHeader->isChecked(); } bool ExportSpreadsheetDialog::commentsAsUnitsFits() const { return ui->chkColumnsAsUnits->isChecked(); } QString ExportSpreadsheetDialog::separator() const { return ui->cbSeparator->currentText(); } QLocale::Language ExportSpreadsheetDialog::numberFormat() const { return (QLocale::Language)ui->cbNumberFormat->currentIndex(); } void ExportSpreadsheetDialog::slotButtonClicked(QAbstractButton* button) { if (button == m_okButton) okClicked(); else if (button == m_cancelButton) { reject(); } } void ExportSpreadsheetDialog::setExportToImage(bool possible) { if (!possible) { ui->cbExportToFITS->setCurrentIndex(1); ui->cbExportToFITS->setItemData(0, 0, Qt::UserRole - 1); } } //SLOTS void ExportSpreadsheetDialog::okClicked() { if (format() != FITS) if ( QFile::exists(ui->leFileName->text()) ) { int r = KMessageBox::questionYesNo(this, i18n("The file already exists. Do you really want to overwrite it?"), i18n("Export")); if (r == KMessageBox::No) return; } KConfigGroup conf(KSharedConfig::openConfig(), "ExportSpreadsheetDialog"); conf.writeEntry("Format", ui->cbFormat->currentIndex()); conf.writeEntry("Header", ui->chkExportHeader->isChecked()); conf.writeEntry("Separator", ui->cbSeparator->currentText()); QString path = ui->leFileName->text(); if (!path.isEmpty()) { QString dir = conf.readEntry("LastDir", ""); - ui->leFileName->setText(path); int pos = path.lastIndexOf(QDir::separator()); if (pos != -1) { QString newDir = path.left(pos); - if (newDir != dir && QDir(newDir).exists()) + if (newDir != dir) conf.writeEntry("LastDir", newDir); } } accept(); } /*! Shows/hides the GroupBox with export options in this dialog. */ void ExportSpreadsheetDialog::toggleOptions() { m_showOptions = !m_showOptions; ui->gbOptions->setVisible(m_showOptions); m_showOptions ? m_showOptionsButton->setText(i18n("Hide Options")) : m_showOptionsButton->setText(i18n("Show Options")); //resize the dialog resize(layout()->minimumSize()); layout()->activate(); resize( QSize(this->width(),0).expandedTo(minimumSize()) ); } /*! opens a file dialog and lets the user select the file. */ void ExportSpreadsheetDialog::selectFile() { KConfigGroup conf(KSharedConfig::openConfig(), "ExportSpreadsheetDialog"); QString dir = conf.readEntry("LastDir", ""); QString extensions; const Format format = (Format)(ui->cbFormat->itemData(ui->cbFormat->currentIndex()).toInt()); switch (format) { case ASCII: extensions = i18n("Text files (*.txt *.dat *.csv)"); break; case Binary: extensions = i18n("Binary files (*.*)"); break; case LaTeX: extensions = i18n("LaTeX files (*.tex)"); break; case FITS: extensions = i18n("FITS files (*.fits *.fit *.fts)"); break; case SQLite: extensions = i18n("SQLite databases files (*.db *.sqlite *.sdb *.db2 *.sqlite2 *.sdb2 *.db3 *.sqlite3 *.sdb3)"); break; } const QString path = QFileDialog::getSaveFileName(this, i18n("Export to file"), dir, extensions); if (!path.isEmpty()) { ui->leFileName->setText(path); int pos = path.lastIndexOf(QDir::separator()); if (pos != -1) { QString newDir = path.left(pos); if (newDir != dir) conf.writeEntry("LastDir", newDir); } } } /*! called when the output format was changed. Adjusts the extension for the specified file. */ void ExportSpreadsheetDialog::formatChanged(int index) { QStringList extensions; extensions << ".txt" << ".bin" << ".tex" << ".fits" << ".db"; QString path = ui->leFileName->text(); int i = path.indexOf("."); if (index != 1) { if (i == -1) path = path + extensions.at(index); else path = path.left(i) + extensions.at(index); } const Format format = (Format)(ui->cbFormat->itemData(ui->cbFormat->currentIndex()).toInt()); if (format == LaTeX) { ui->cbSeparator->hide(); ui->lSeparator->hide(); ui->lNumberFormat->hide(); ui->cbNumberFormat->hide(); ui->chkCaptions->show(); ui->chkGridLines->show(); ui->lExportArea->show(); ui->lGridLines->show(); ui->lCaptions->show(); ui->cbLaTeXExport->show(); if (!m_matrixMode) { ui->lHeader->show(); ui->chkHeaders->show(); ui->lEmptyRows->show(); ui->chkEmptyRows->show(); ui->lMatrixHHeader->hide(); ui->lMatrixVHeader->hide(); ui->chkMatrixHHeader->hide(); ui->chkMatrixVHeader->hide(); } else { ui->lMatrixHHeader->show(); ui->lMatrixVHeader->show(); ui->chkMatrixHHeader->show(); ui->chkMatrixVHeader->show(); } ui->cbExportToFITS->hide(); ui->lExportToFITS->hide(); ui->lColumnAsUnits->hide(); ui->chkColumnsAsUnits->hide(); } else if (format == FITS) { ui->lCaptions->hide(); ui->lEmptyRows->hide(); ui->lExportArea->hide(); ui->lGridLines->hide(); ui->lMatrixHHeader->hide(); ui->lMatrixVHeader->hide(); ui->lSeparator->hide(); ui->lHeader->hide(); ui->chkEmptyRows->hide(); ui->chkHeaders->hide(); ui->chkExportHeader->hide(); ui->lExportHeader->hide(); ui->chkGridLines->hide(); ui->chkMatrixHHeader->hide(); ui->chkMatrixVHeader->hide(); ui->chkCaptions->hide(); ui->cbLaTeXExport->hide(); ui->cbSeparator->hide(); ui->lNumberFormat->hide(); ui->cbNumberFormat->hide(); ui->cbExportToFITS->show(); ui->lExportToFITS->show(); if (!m_matrixMode) { if (ui->cbExportToFITS->currentIndex() == 1) { ui->lColumnAsUnits->show(); ui->chkColumnsAsUnits->show(); } } } else if (format == SQLite) { ui->cbSeparator->hide(); ui->lSeparator->hide(); ui->lNumberFormat->hide(); ui->cbNumberFormat->hide(); ui->chkCaptions->hide(); ui->chkEmptyRows->hide(); ui->chkGridLines->hide(); ui->lEmptyRows->hide(); ui->lExportArea->hide(); ui->lGridLines->hide(); ui->lCaptions->hide(); ui->cbLaTeXExport->hide(); ui->cbLaTeXExport->hide(); ui->lMatrixHHeader->hide(); ui->lMatrixVHeader->hide(); ui->chkMatrixHHeader->hide(); ui->chkMatrixVHeader->hide(); ui->lHeader->hide(); ui->chkHeaders->hide(); ui->chkExportHeader->hide(); ui->lExportHeader->hide(); ui->cbExportToFITS->hide(); ui->lExportToFITS->hide(); ui->lColumnAsUnits->hide(); ui->chkColumnsAsUnits->hide(); } else { ui->cbSeparator->show(); ui->lSeparator->show(); ui->lNumberFormat->show(); ui->cbNumberFormat->show(); ui->chkCaptions->hide(); ui->chkEmptyRows->hide(); ui->chkGridLines->hide(); ui->lEmptyRows->hide(); ui->lExportArea->hide(); ui->lGridLines->hide(); ui->lCaptions->hide(); ui->cbLaTeXExport->hide(); ui->lMatrixHHeader->hide(); ui->lMatrixVHeader->hide(); ui->chkMatrixHHeader->hide(); ui->chkMatrixVHeader->hide(); ui->lHeader->hide(); ui->chkHeaders->hide(); ui->cbExportToFITS->hide(); ui->lExportToFITS->hide(); ui->lColumnAsUnits->hide(); ui->chkColumnsAsUnits->hide(); } if (!m_matrixMode && !(format == FITS || format == SQLite)) { ui->chkExportHeader->show(); ui->lExportHeader->show(); } setFormat(static_cast(index)); ui->leFileName->setText(path); } void ExportSpreadsheetDialog::setExportSelection(bool enable) { if (!enable) { const auto* areaToExportModel = qobject_cast(ui->cbLaTeXExport->model()); QStandardItem* item = areaToExportModel->item(1); item->setFlags(item->flags() & ~(Qt::ItemIsSelectable|Qt::ItemIsEnabled)); } } void ExportSpreadsheetDialog::setFormat(Format format) { m_format = format; } void ExportSpreadsheetDialog::setExportTo(const QStringList &to) { ui->cbExportToFITS->addItems(to); } ExportSpreadsheetDialog::Format ExportSpreadsheetDialog::format() const { return m_format; } void ExportSpreadsheetDialog::fileNameChanged(const QString& name) { - m_okButton->setEnabled(!name.simplified().isEmpty()); + if (name.simplified().isEmpty()) { + m_okButton->setEnabled(false); + return; + } + QString path = ui->leFileName->text(); + int pos = path.lastIndexOf(QDir::separator()); + if (pos != -1) { + QString dir = path.left(pos); + if (!QDir(dir).exists()) { + ui->leFileName->setStyleSheet("QLineEdit{background:red;}"); + m_okButton->setEnabled(false); + return; + } else + ui->leFileName->setStyleSheet(QString()); + } + + m_okButton->setEnabled(true); } diff --git a/src/kdefrontend/worksheet/ExportWorksheetDialog.cpp b/src/kdefrontend/worksheet/ExportWorksheetDialog.cpp index 9e9893a3f..3e68e931a 100644 --- a/src/kdefrontend/worksheet/ExportWorksheetDialog.cpp +++ b/src/kdefrontend/worksheet/ExportWorksheetDialog.cpp @@ -1,292 +1,308 @@ /*************************************************************************** File : ExportWorksheetDialog.cpp Project : LabPlot Description : export worksheet dialog -------------------------------------------------------------------- Copyright : (C) 2011-2019 by Alexander Semke (alexander.semke@web.de) ***************************************************************************/ /*************************************************************************** * * * This program 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. * * * * This program 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, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * ***************************************************************************/ #include "ExportWorksheetDialog.h" #include "ui_exportworksheetwidget.h" #include #include #include #include #include #include #include #include #include #include /*! \class ExportWorksheetDialog \brief Dialog for exporting a worksheet to a file. \ingroup kdefrontend */ ExportWorksheetDialog::ExportWorksheetDialog(QWidget* parent) : QDialog(parent), ui(new Ui::ExportWorksheetWidget()) { ui->setupUi(this); QDialogButtonBox* btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); m_showOptionsButton = new QPushButton; connect(btnBox, &QDialogButtonBox::clicked, this, &ExportWorksheetDialog::slotButtonClicked); btnBox->addButton(m_showOptionsButton, QDialogButtonBox::ActionRole); ui->verticalLayout->addWidget(btnBox); m_okButton = btnBox->button(QDialogButtonBox::Ok); m_cancelButton = btnBox->button(QDialogButtonBox::Cancel); m_cancelButton->setToolTip(i18n("Close this dialog without exporting.")); ui->leFileName->setCompleter(new QCompleter(new QDirModel, this)); ui->bOpen->setIcon(QIcon::fromTheme(QLatin1String("document-open"))); ui->cbFormat->addItem(QIcon::fromTheme(QLatin1String("application-pdf")), QLatin1String("Portable Data Format (PDF)")); ui->cbFormat->addItem(QIcon::fromTheme(QLatin1String("image-svg+xml")), QLatin1String("Scalable Vector Graphics (SVG)")); ui->cbFormat->insertSeparator(3); ui->cbFormat->addItem(QIcon::fromTheme(QLatin1String("image-x-generic")), QLatin1String("Portable Network Graphics (PNG)")); ui->cbExportTo->addItem(i18n("File")); ui->cbExportTo->addItem(i18n("Clipboard")); ui->cbExportArea->addItem(i18n("Object's bounding box")); ui->cbExportArea->addItem(i18n("Current selection")); ui->cbExportArea->addItem(i18n("Complete worksheet")); ui->cbResolution->addItem(i18nc("%1 is the value of DPI of the current screen", "%1 (desktop)", QString::number(QApplication::desktop()->physicalDpiX()))); ui->cbResolution->addItem(QLatin1String("100")); ui->cbResolution->addItem(QLatin1String("150")); ui->cbResolution->addItem(QLatin1String("200")); ui->cbResolution->addItem(QLatin1String("300")); ui->cbResolution->addItem(QLatin1String("600")); ui->cbResolution->setValidator(new QIntValidator(ui->cbResolution)); connect(ui->cbFormat, static_cast(&KComboBox::currentIndexChanged), this, &ExportWorksheetDialog::formatChanged); connect(ui->cbExportTo, static_cast(&KComboBox::currentIndexChanged), this, &ExportWorksheetDialog::exportToChanged); connect(ui->bOpen, &QPushButton::clicked, this, &ExportWorksheetDialog::selectFile); connect(ui->leFileName, &QLineEdit::textChanged, this, &ExportWorksheetDialog::fileNameChanged); connect(m_showOptionsButton, &QPushButton::clicked, this, &ExportWorksheetDialog::toggleOptions); ui->leFileName->setFocus(); setWindowTitle(i18nc("@title:window", "Export Worksheet")); setWindowIcon(QIcon::fromTheme(QLatin1String("document-export-database"))); //restore saved settings if available KConfigGroup conf(KSharedConfig::openConfig(), "ExportWorksheetDialog"); ui->cbFormat->setCurrentIndex(conf.readEntry("Format", 0)); ui->cbExportTo->setCurrentIndex(conf.readEntry("ExportTo", 0)); ui->cbExportArea->setCurrentIndex(conf.readEntry("Area", 0)); ui->chkExportBackground->setChecked(conf.readEntry("Background", true)); ui->cbResolution->setCurrentIndex(conf.readEntry("Resolution", 0)); m_showOptions = conf.readEntry("ShowOptions", true); m_showOptions ? m_showOptionsButton->setText(i18n("Hide Options")) : m_showOptionsButton->setText(i18n("Show Options")); ui->gbOptions->setVisible(m_showOptions); create(); // ensure there's a window created if (conf.exists()) { KWindowConfig::restoreWindowSize(windowHandle(), conf); resize(windowHandle()->size()); // workaround for QTBUG-40584 } else resize(QSize(0, 0).expandedTo(minimumSize())); } ExportWorksheetDialog::~ExportWorksheetDialog() { //save current settings KConfigGroup conf(KSharedConfig::openConfig(), "ExportWorksheetDialog"); conf.writeEntry("Format", ui->cbFormat->currentIndex()); conf.writeEntry("ExportTo", ui->cbExportTo->currentIndex()); conf.writeEntry("Area", ui->cbExportArea->currentIndex()); conf.writeEntry("Background", ui->chkExportBackground->isChecked()); conf.writeEntry("Resolution", ui->cbResolution->currentIndex()); conf.writeEntry("ShowOptions", m_showOptions); KWindowConfig::saveWindowSize(windowHandle(), conf); } void ExportWorksheetDialog::setFileName(const QString& name) { KConfigGroup conf(KSharedConfig::openConfig(), "ExportWorksheetDialog"); QString dir = conf.readEntry("LastDir", ""); if (dir.isEmpty()) dir = QDir::homePath(); ui->leFileName->setText(dir + QDir::separator() + name); formatChanged(ui->cbFormat->currentIndex()); exportToChanged(ui->cbExportTo->currentIndex()); } QString ExportWorksheetDialog::path() const { if (ui->cbExportTo->currentIndex() == 0) return ui->leFileName->text(); else return QString(); } WorksheetView::ExportFormat ExportWorksheetDialog::exportFormat() const { int index = ui->cbFormat->currentIndex(); //we have a separator in the format combobox at the 3th position -> skip it if (index > 2) index--; return WorksheetView::ExportFormat(index); } WorksheetView::ExportArea ExportWorksheetDialog::exportArea() const { return WorksheetView::ExportArea(ui->cbExportArea->currentIndex()); } bool ExportWorksheetDialog::exportBackground() const { return ui->chkExportBackground->isChecked(); } int ExportWorksheetDialog::exportResolution() const { if (ui->cbResolution->currentIndex() == 0) return QApplication::desktop()->physicalDpiX(); else return ui->cbResolution->currentText().toInt(); } void ExportWorksheetDialog::slotButtonClicked(QAbstractButton* button) { if (button == m_okButton) okClicked(); else if (button == m_cancelButton) reject(); } //SLOTS void ExportWorksheetDialog::okClicked() { if ( QFile::exists(ui->leFileName->text()) ) { int r = KMessageBox::questionYesNo(this, i18n("The file already exists. Do you really want to overwrite it?"), i18n("Export")); if (r == KMessageBox::No) return; } KConfigGroup conf(KSharedConfig::openConfig(), "ExportWorksheetDialog"); QString path = ui->leFileName->text(); if (!path.isEmpty()) { QString dir = conf.readEntry("LastDir", ""); - ui->leFileName->setText(path); int pos = path.lastIndexOf(QDir::separator()); if (pos != -1) { QString newDir = path.left(pos); if (newDir != dir) conf.writeEntry("LastDir", newDir); } } accept(); } /*! Shows/hides the GroupBox with export options in this dialog. */ void ExportWorksheetDialog::toggleOptions() { m_showOptions = !m_showOptions; ui->gbOptions->setVisible(m_showOptions); m_showOptions ? m_showOptionsButton->setText(i18n("Hide Options")) : m_showOptionsButton->setText(i18n("Show Options")); //resize the dialog layout()->activate(); resize( QSize(this->width(), 0).expandedTo(minimumSize()) ); } /*! opens a file dialog and lets the user select the file. */ void ExportWorksheetDialog::selectFile() { KConfigGroup conf(KSharedConfig::openConfig(), "ExportWorksheetDialog"); const QString dir = conf.readEntry("LastDir", ""); QString format; if (ui->cbFormat->currentIndex() == 0) format = i18n("Portable Data Format (PDF) (*.pdf *.PDF)"); else if (ui->cbFormat->currentIndex() == 1) format = i18n("Scalable Vector Graphics (SVG) (*.svg *.SVG)"); else format = i18n("Portable Network Graphics (PNG) (*.png *.PNG)"); const QString path = QFileDialog::getSaveFileName(this, i18n("Export to file"), dir, format); if (!path.isEmpty()) { ui->leFileName->setText(path); int pos = path.lastIndexOf(QDir::separator()); if (pos != -1) { const QString newDir = path.left(pos); - if (newDir != dir) + if (newDir != dir && QDir(newDir).exists()) conf.writeEntry("LastDir", newDir); } } } /*! called when the output format was changed. Adjusts the extension for the specified file. */ void ExportWorksheetDialog::formatChanged(int index) { //we have a separator in the format combobox at the 3rd position -> skip it if (index > 2) index --; QStringList extensions; extensions << QLatin1String(".pdf") << QLatin1String(".svg") << QLatin1String(".png"); QString path = ui->leFileName->text(); int i = path.indexOf(QLatin1Char('.')); if (i == -1) path = path + extensions.at(index); else path = path.left(i) + extensions.at(index); ui->leFileName->setText(path); // show resolution option for png format bool visible = (index == 2); ui->lResolution->setVisible(visible); ui->cbResolution->setVisible(visible); } + /*! called when the target destination (file or clipboard) format was changed. */ void ExportWorksheetDialog::exportToChanged(int index) { bool toFile = (index == 0); ui->lFileName->setVisible(toFile); ui->leFileName->setVisible(toFile); ui->bOpen->setVisible(toFile); if (toFile) m_okButton->setToolTip(i18n("Export to file and close the dialog.")); else m_okButton->setToolTip(i18n("Export to clipboard and close the dialog.")); } void ExportWorksheetDialog::fileNameChanged(const QString& name) { - m_okButton->setEnabled(!name.simplified().isEmpty()); + if (name.simplified().isEmpty()) { + m_okButton->setEnabled(false); + return; + } + QString path = ui->leFileName->text(); + int pos = path.lastIndexOf(QDir::separator()); + if (pos != -1) { + QString dir = path.left(pos); + if (!QDir(dir).exists()) { + ui->leFileName->setStyleSheet("QLineEdit{background:red;}"); + m_okButton->setEnabled(false); + return; + } else + ui->leFileName->setStyleSheet(QString()); + } + + m_okButton->setEnabled(true); }