| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2002 Jürgen Riegel <[email protected]> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
#include <QDir> |
| 24 |
#include <QFile> |
| 25 |
#include <QFileInfo> |
| 26 |
#include <QMessageBox> |
| 27 |
|
| 28 |
#include "Dialogs/DlgMacroRecordImp.h" |
| 29 |
#include "ui_DlgMacroRecord.h" |
| 30 |
#include "Application.h" |
| 31 |
#include "FileDialog.h" |
| 32 |
#include "Macro.h" |
| 33 |
#include "MainWindow.h" |
| 34 |
|
| 35 |
|
| 36 |
using namespace Gui::Dialog; |
| 37 |
|
| 38 |
/* TRANSLATOR Gui::Dialog::DlgMacroRecordImp */ |
| 39 |
|
| 40 |
/** |
| 41 |
* Constructs a DlgMacroRecordImp which is a child of 'parent', with the |
| 42 |
* name 'name' and widget flags set to 'f' |
| 43 |
* |
| 44 |
* The dialog will by default be modeless, unless you set 'modal' to |
| 45 |
* true to construct a modal dialog. |
| 46 |
*/ |
| 47 |
DlgMacroRecordImp::DlgMacroRecordImp(QWidget* parent, Qt::WindowFlags fl) |
| 48 |
: QDialog(parent, fl) |
| 49 |
, WindowParameter("Macro") |
| 50 |
, ui(new Ui_DlgMacroRecord) |
| 51 |
{ |
| 52 |
ui->setupUi(this); |
| 53 |
setupConnections(); |
| 54 |
|
| 55 |
// get the macro home path |
| 56 |
this->macroPath = QString::fromUtf8( |
| 57 |
getWindowParameter()->GetASCII("MacroPath", App::Application::getUserMacroDir().c_str()).c_str() |
| 58 |
); |
| 59 |
this->macroPath = QDir::toNativeSeparators(QDir(this->macroPath).path() + QDir::separator()); |
| 60 |
|
| 61 |
// set the edit fields |
| 62 |
ui->lineEditMacroPath->setText(macroPath); |
| 63 |
|
| 64 |
// get a pointer to the macro manager |
| 65 |
this->macroManager = Application::Instance->macroManager(); |
| 66 |
|
| 67 |
// check if a macro recording is in progress |
| 68 |
this->macroManager->isOpen() ? ui->buttonStart->setEnabled(false) |
| 69 |
: ui->buttonStop->setEnabled(false); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Destroys the object and frees any allocated resources |
| 74 |
*/ |
| 75 |
DlgMacroRecordImp::~DlgMacroRecordImp() = default; |
| 76 |
|
| 77 |
void DlgMacroRecordImp::setupConnections() |
| 78 |
{ |
| 79 |
// clang-format off |
| 80 |
connect(ui->buttonStart, &QPushButton::clicked, |
| 81 |
this, &DlgMacroRecordImp::onButtonStartClicked); |
| 82 |
connect(ui->buttonStop, &QPushButton::clicked, |
| 83 |
this, &DlgMacroRecordImp::onButtonStopClicked); |
| 84 |
connect(ui->buttonClose, &QPushButton::clicked, |
| 85 |
this, &DlgMacroRecordImp::onButtonCloseClicked); |
| 86 |
connect(ui->pushButtonChooseDir, &QPushButton::clicked, |
| 87 |
this, &DlgMacroRecordImp::onButtonChooseDirClicked); |
| 88 |
connect(ui->lineEditMacroPath, &QLineEdit::textChanged, |
| 89 |
this, &DlgMacroRecordImp::onMacroPathTextChanged); |
| 90 |
// clang-format on |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Starts the record of the macro. |
| 95 |
*/ |
| 96 |
void DlgMacroRecordImp::onButtonStartClicked() |
| 97 |
{ |
| 98 |
// test if the path already set |
| 99 |
if (ui->lineEditPath->text().isEmpty()) { |
| 100 |
QMessageBox::information( |
| 101 |
getMainWindow(), |
| 102 |
tr("Macro recorder"), |
| 103 |
tr("Specify a place to save first.") |
| 104 |
); |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
QDir dir(macroPath); |
| 109 |
if (!dir.exists()) { |
| 110 |
QMessageBox::information( |
| 111 |
getMainWindow(), |
| 112 |
tr("Macro recorder"), |
| 113 |
tr("The macro directory does not exist. Choose another one.") |
| 114 |
); |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
// search in the macro path first for an already existing macro |
| 119 |
QString fn = this->macroPath + ui->lineEditPath->text(); |
| 120 |
if (!fn.endsWith(QLatin1String(".FCMacro"))) { |
| 121 |
fn += QLatin1String(".FCMacro"); |
| 122 |
} |
| 123 |
|
| 124 |
QFileInfo fi(fn); |
| 125 |
if (fi.isFile() && fi.exists()) { |
| 126 |
if (QMessageBox::question( |
| 127 |
this, |
| 128 |
tr("Existing macro"), |
| 129 |
tr("The macro '%1' already exists. Overwrite it?").arg(fn), |
| 130 |
QMessageBox::Yes | QMessageBox::No, |
| 131 |
QMessageBox::No |
| 132 |
) |
| 133 |
== QMessageBox::No) { |
| 134 |
return; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
QFile file(fn); |
| 139 |
if (!file.open(QFile::WriteOnly)) { |
| 140 |
QMessageBox::information( |
| 141 |
getMainWindow(), |
| 142 |
tr("Macro recorder"), |
| 143 |
tr("You have no write permission for the directory. Choose another one.") |
| 144 |
); |
| 145 |
return; |
| 146 |
} |
| 147 |
file.close(); |
| 148 |
|
| 149 |
// open the macro recording |
| 150 |
this->macroManager->open(MacroManager::File, fn.toUtf8().constData()); |
| 151 |
|
| 152 |
ui->buttonStart->setEnabled(false); |
| 153 |
ui->buttonStop->setEnabled(true); |
| 154 |
ui->buttonClose->setEnabled(false); |
| 155 |
QDialog::accept(); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Abort the macro. |
| 160 |
*/ |
| 161 |
void DlgMacroRecordImp::onButtonCloseClicked() |
| 162 |
{ |
| 163 |
if (this->macroManager->isOpen()) { |
| 164 |
this->macroManager->cancel(); |
| 165 |
} |
| 166 |
|
| 167 |
QDialog::reject(); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Stops the record of the macro and save to the file. |
| 172 |
*/ |
| 173 |
void DlgMacroRecordImp::onButtonStopClicked() |
| 174 |
{ |
| 175 |
if (this->macroManager->isOpen()) { |
| 176 |
// ends the macrorecording and save the file... |
| 177 |
this->macroManager->commit(); |
| 178 |
} |
| 179 |
|
| 180 |
ui->buttonStart->setEnabled(true); |
| 181 |
ui->buttonStop->setEnabled(false); |
| 182 |
ui->buttonClose->setEnabled(true); |
| 183 |
QDialog::accept(); |
| 184 |
} |
| 185 |
|
| 186 |
void DlgMacroRecordImp::onButtonChooseDirClicked() |
| 187 |
{ |
| 188 |
QString newDir |
| 189 |
= QFileDialog::getExistingDirectory(nullptr, tr("Choose macro directory"), macroPath); |
| 190 |
if (!newDir.isEmpty()) { |
| 191 |
macroPath = QDir::toNativeSeparators(newDir + QDir::separator()); |
| 192 |
ui->lineEditMacroPath->setText(macroPath); |
| 193 |
|
| 194 |
std::filesystem::path chosenPath(macroPath.toStdString()); |
| 195 |
if (chosenPath.filename().empty()) { |
| 196 |
chosenPath = chosenPath.parent_path(); |
| 197 |
} |
| 198 |
std::filesystem::path userMacroDir(App::Application::getUserMacroDir()); |
| 199 |
if (userMacroDir.filename().empty()) { |
| 200 |
userMacroDir = userMacroDir.parent_path(); |
| 201 |
} |
| 202 |
if (chosenPath != userMacroDir) { |
| 203 |
getWindowParameter()->SetASCII("MacroPath", macroPath.toUtf8()); |
| 204 |
} |
| 205 |
else if (getWindowParameter()->GetASCII("MacroPath", "UNSET") != "UNSET") { |
| 206 |
// If the new path IS the default path, remove any existing storage of the path |
| 207 |
getWindowParameter()->RemoveASCII("MacroPath"); |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
void DlgMacroRecordImp::onMacroPathTextChanged(const QString& newDir) |
| 213 |
{ |
| 214 |
macroPath = newDir; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
#include "moc_DlgMacroRecordImp.cpp" |