1
/*******************************************************************
3
Part of the Fritzing project - http://fritzing.org
4
Copyright (c) 2007-2012 Fachhochschule Potsdam - http://fh-potsdam.de
6
Fritzing is free software: you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation, either version 3 of the License, or
9
(at your option) any later version.
11
Fritzing 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 General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with Fritzing. If not, see <http://www.gnu.org/licenses/>.
19
********************************************************************
22
$Author: irascibl@gmail.com $:
23
$Date: 2012-09-30 02:00:34 +0200 (Sun, 30 Sep 2012) $
25
********************************************************************/
30
#include <QMessageBox>
31
#include <QFileDialog>
34
#include <QAbstractButton>
37
#include "fritzingwindow.h"
38
#include "../debugdialog.h"
39
#include "../utils/misc.h"
40
#include "../utils/folderutils.h"
41
#include "../utils/fileprogressdialog.h"
43
const QString FritzingWindow::QtFunkyPlaceholder = QLatin1String("[*]"); // this is some weird hack Qt uses in window titles as a placeholder to setr the modified state
44
QString FritzingWindow::ReadOnlyPlaceholder(" [READ-ONLY] ");
45
static QString ___fritzingTitle___;
46
QStringList FritzingWindow::OtherKnownExtensions;
48
FritzingWindow::FritzingWindow(const QString &untitledFileName, int &untitledFileCount, QString fileExt, QWidget * parent, Qt::WFlags f)
49
: QMainWindow(parent, f)
51
___fritzingTitle___ = QObject::tr("Fritzing");
55
this->setWindowIcon(QIcon(QPixmap(":resources/images/fritzing_icon.png")));
57
QString fn = untitledFileName;
59
if(untitledFileCount > 1) {
60
fn += " " + QString::number(untitledFileCount);
69
m_undoStack = new WaitPushUndoStack(this);
70
connect(m_undoStack, SIGNAL(cleanChanged(bool)), this, SLOT(undoStackCleanChanged(bool)) );
73
void FritzingWindow::createCloseAction() {
74
m_closeAct = new QAction(tr("&Close Window"), this);
75
m_closeAct->setShortcut(tr("Ctrl+W"));
76
m_closeAct->setStatusTip(tr("Close the current sketch"));
77
connect(m_closeAct, SIGNAL(triggered()), this, SLOT(close()));
80
void FritzingWindow::setTitle() {
81
setWindowTitle(tr("%1 - %2")
82
.arg(QFileInfo(m_fwFilename).fileName()+(m_readOnly?ReadOnlyPlaceholder:"")+QtFunkyPlaceholder)
83
.arg(fritzingTitle()));
86
// returns true if the user wanted to save the file
87
bool FritzingWindow::save() {
89
if (FolderUtils::isEmptyFileName(m_fwFilename, untitledFileName())) {
91
} else if (m_readOnly) {
94
result = saveAsAux(m_fwFilename);
100
bool FritzingWindow::saveAs() {
101
return saveAs(m_fwFilename, m_readOnly);
104
bool FritzingWindow::save(const QString & filename, bool readOnly) {
105
if (FolderUtils::isEmptyFileName(filename, untitledFileName())) {
106
return saveAs(filename, readOnly);
107
} else if (m_readOnly) {
108
return saveAs(filename, readOnly);
110
return saveAsAux(filename);
114
bool FritzingWindow::saveAs(const QString & filename, bool readOnly) {
115
DebugDialog::debug(QString("current path: %1").arg(QDir::currentPath()));
118
QString untitledBase = untitledFileName();
121
path = defaultSaveFolder() + "/" + QFileInfo(filename).fileName();
123
else if(filename.startsWith(untitledBase, Qt::CaseInsensitive)) {
124
path = defaultSaveFolder() + "/" + filename;
126
else if(filename.isNull() || filename.isEmpty()) {
127
path = defaultSaveFolder();
132
DebugDialog::debug(QString("current file: %1").arg(filename));
133
QString newFilename = FolderUtils::getSaveFileName(
135
tr("Specify a file name"),
137
getExtensionString(),
141
if (newFilename.isEmpty()) return false; // Cancel pressed
143
if (readOnly && (newFilename.compare(filename, Qt::CaseInsensitive) == 0)) {
144
QMessageBox::warning(NULL, QObject::tr("Fritzing"),
145
QObject::tr("The file '%1' is read-only; please use a different filename.")
151
FileProgressDialog progress("Saving...", 0, this);
153
QStringList extensions = getExtensions();
154
bool hasExtension = false;
155
foreach (QString extension, extensions) {
156
if(alreadyHasExtension(newFilename, extension)) {
163
newFilename += extensions[0];
166
return saveAsAux(newFilename);
169
void FritzingWindow::undoStackCleanChanged(bool isClean) {
170
setWindowModified(!isClean);
173
bool FritzingWindow::alreadyHasExtension(const QString &fileName, const QString &fileExt) {
174
// TODO: Make something prettier to manage all the supported formats at once
175
if(!fileExt.isEmpty()) {
176
return fileName.endsWith(fileExt);
178
foreach (QString extension, fritzingExtensions()) {
179
if (fileName.endsWith(extension)) return true;
181
foreach (QString extension, OtherKnownExtensions) {
182
if (fileName.endsWith(extension)) return true;
189
bool FritzingWindow::anyModified() {
190
return isWindowModified();
193
bool FritzingWindow::beforeClosing(bool showCancel, bool & discard) {
196
QMessageBox::StandardButton reply = beforeClosingMessage(m_fwFilename, showCancel);
197
if (reply == QMessageBox::Save) {
200
else if (reply == QMessageBox::Discard) {
213
QMessageBox::StandardButton FritzingWindow::beforeClosingMessage(const QString & filename, bool showCancel)
216
QMessageBox messageBox(this);
217
setBeforeClosingText(filename, messageBox);
218
QMessageBox::StandardButtons buttons = QMessageBox::Save | QMessageBox::Discard;
220
buttons |= QMessageBox::Cancel;
222
messageBox.setStandardButtons(buttons);
223
messageBox.setDefaultButton(QMessageBox::Save);
224
if (m_fwFilename.startsWith(untitledFileName())) {
225
messageBox.setButtonText(QMessageBox::Save, tr("Save..."));
228
messageBox.setButtonText(QMessageBox::Save, tr("Save"));
230
messageBox.setButtonText(QMessageBox::Discard, tr("Don't Save"));
232
messageBox.setButtonText(QMessageBox::Cancel, tr("Cancel"));
234
messageBox.setIcon(QMessageBox::Warning);
235
messageBox.setWindowModality(Qt::WindowModal);
236
messageBox.button(QMessageBox::Discard)->setShortcut(tr("Ctrl+D"));
238
return (QMessageBox::StandardButton) messageBox.exec();
241
void FritzingWindow::setBeforeClosingText(const QString & filename, QMessageBox & messageBox)
243
QString basename = QFileInfo(filename).baseName();
244
messageBox.setWindowTitle(tr("Save \"%1\"").arg(basename));
245
messageBox.setText(tr("Do you want to save the changes you made in the document \"%1\"?").arg(basename));
246
messageBox.setInformativeText(tr("Your changes will be lost if you don't save them."));
249
void FritzingWindow::setReadOnly(bool readOnly) {
250
bool hasChanged = m_readOnly != readOnly;
251
m_readOnly = readOnly;
253
emit readOnlyChanged(readOnly);
257
const QString FritzingWindow::fritzingTitle() {
258
return ___fritzingTitle___;
261
const QString & FritzingWindow::fileName() {
265
void FritzingWindow::setFileName(const QString & filename) {
266
m_fwFilename = filename;
269
void FritzingWindow::notClosableForAWhile() {