Removed the Critical and Question functions, replaced with direct QMessageBox uses. Removed unused code from mainwindow.h.

Thu, 23 Feb 2017 19:56:21 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 23 Feb 2017 19:56:21 +0200
changeset 1156
c20ee66b6705
parent 1155
b0e004c8e3a4
child 1157
1d6d244bdabd

Removed the Critical and Question functions, replaced with direct QMessageBox uses. Removed unused code from mainwindow.h.

src/dialogs/configdialog.h file | annotate | diff | comparison | revisions
src/documentmanager.cpp file | annotate | diff | comparison | revisions
src/lddocument.cpp file | annotate | diff | comparison | revisions
src/mainwindow.cpp file | annotate | diff | comparison | revisions
src/mainwindow.h file | annotate | diff | comparison | revisions
src/partdownloader.cpp file | annotate | diff | comparison | revisions
src/partdownloadrequest.cpp file | annotate | diff | comparison | revisions
src/primitives.cpp file | annotate | diff | comparison | revisions
src/toolsets/algorithmtoolset.cpp file | annotate | diff | comparison | revisions
src/toolsets/extprogramtoolset.cpp file | annotate | diff | comparison | revisions
src/toolsets/filetoolset.cpp file | annotate | diff | comparison | revisions
src/toolsets/viewtoolset.cpp file | annotate | diff | comparison | revisions
--- a/src/dialogs/configdialog.h	Thu Feb 23 19:33:56 2017 +0200
+++ b/src/dialogs/configdialog.h	Thu Feb 23 19:56:21 2017 +0200
@@ -21,9 +21,6 @@
 #include "../toolsets/extprogramtoolset.h"
 #include <QDialog>
 
-class QLabel;
-class QDoubleSpinBox;
-
 // =============================================================================
 class ShortcutListItem : public QListWidgetItem
 {
@@ -116,8 +113,8 @@
 	explicit KeySequenceDialog (QKeySequence seq, QWidget* parent = nullptr, Qt::WindowFlags f = 0);
 	static bool staticDialog (ShortcutListItem* item, QWidget* parent = nullptr);
 
-	QLabel* lb_output;
-	QDialogButtonBox* bbx_buttons;
+	class QLabel* lb_output;
+	class QDialogButtonBox* bbx_buttons;
 	QKeySequence seq;
 
 private:
--- a/src/documentmanager.cpp	Thu Feb 23 19:33:56 2017 +0200
+++ b/src/documentmanager.cpp	Thu Feb 23 19:56:21 2017 +0200
@@ -18,6 +18,7 @@
 
 #include <QApplication>
 #include <QFileInfo>
+#include <QMessageBox>
 #include "documentmanager.h"
 #include "lddocument.h"
 #include "mainwindow.h"
@@ -111,7 +112,7 @@
 		{
 			// Tell the user loading failed.
 			setlocale (LC_ALL, "C");
-			Critical (format (tr ("Failed to open %1: %2"), path, strerror (errno)));
+			QMessageBox::critical(m_window, tr("Error"), format(tr("Failed to open %1: %2"), path, strerror (errno)));
 		}
 
 		m_loadingMainFile = false;
--- a/src/lddocument.cpp	Thu Feb 23 19:33:56 2017 +0200
+++ b/src/lddocument.cpp	Thu Feb 23 19:56:21 2017 +0200
@@ -151,20 +151,19 @@
 //
 bool LDDocument::isSafeToClose()
 {
-	using msgbox = QMessageBox;
 	setlocale (LC_ALL, "C");
 
 	// If we have unsaved changes, warn and give the option of saving.
 	if (hasUnsavedChanges())
 	{
-		QString message = format (tr ("There are unsaved changes to %1. Should it be saved?"), getDisplayName());
+		QString message = format(tr("There are unsaved changes to %1. Should it be saved?"), getDisplayName());
 
-		int button = msgbox::question (m_window, QObject::tr ("Unsaved Changes"), message,
-			(msgbox::Yes | msgbox::No | msgbox::Cancel), msgbox::Cancel);
+		int button = QMessageBox::question (m_window, tr("Unsaved Changes"), message,
+		    (QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel), QMessageBox::Cancel);
 
 		switch (button)
 		{
-			case msgbox::Yes:
+		    case QMessageBox::Yes:
 			{
 				// If we don't have a file path yet, we have to ask the user for one.
 				if (name().isEmpty())
@@ -183,8 +182,8 @@
 					message = format (QObject::tr ("Failed to save %1 (%2)\nDo you still want to close?"),
 						name(), strerror (errno));
 
-					if (msgbox::critical (m_window, QObject::tr ("Save Failure"), message,
-						(msgbox::Yes | msgbox::No), msgbox::No) == msgbox::No)
+					if (QMessageBox::critical (m_window, tr("Save Failure"), message,
+					    (QMessageBox::Yes | QMessageBox::No), QMessageBox::No) == QMessageBox::No)
 					{
 						return false;
 					}
@@ -192,7 +191,7 @@
 				break;
 			}
 
-			case msgbox::Cancel:
+		    case QMessageBox::Cancel:
 				return false;
 
 			default:
--- a/src/mainwindow.cpp	Thu Feb 23 19:33:56 2017 +0200
+++ b/src/mainwindow.cpp	Thu Feb 23 19:56:21 2017 +0200
@@ -780,37 +780,6 @@
 	return (QPixmap (format (":/icons/%1.png", iconName)));
 }
 
-// ---------------------------------------------------------------------------------------------------------------------
-//
-bool Confirm (const QString& message)
-{
-	return Confirm (MainWindow::tr ("Confirm"), message);
-}
-
-// ---------------------------------------------------------------------------------------------------------------------
-//
-bool Confirm (const QString& title, const QString& message)
-{
-	return QMessageBox::question (g_win, title, message,
-		(QMessageBox::Yes | QMessageBox::No), QMessageBox::No) == QMessageBox::Yes;
-}
-
-// ---------------------------------------------------------------------------------------------------------------------
-//
-void Critical (const QString& message)
-{
-	QMessageBox::critical (g_win, MainWindow::tr ("Error"), message,
-		(QMessageBox::Close), QMessageBox::Close);
-}
-
-// ---------------------------------------------------------------------------------------------------------------------
-//
-void errorPrompt (QWidget* parent, const QString& message)
-{
-	QMessageBox::critical (parent, MainWindow::tr ("Error"), message,
-		(QMessageBox::Close), QMessageBox::Close);
-}
-
 MessageManager* MainWindow::messageLog() const
 {
 	return m_messageLog;
--- a/src/mainwindow.h	Thu Feb 23 19:33:56 2017 +0200
+++ b/src/mainwindow.h	Thu Feb 23 19:56:21 2017 +0200
@@ -29,12 +29,8 @@
 #include "doublemap.h"
 
 class MessageManager;
-class MainWindow;
 class QToolButton;
-class QDialogButtonBox;
 class Canvas;
-class QComboBox;
-class QProgressBar;
 class Toolset;
 class Configuration;
 class PrimitiveManager;
@@ -176,46 +172,4 @@
 
 // Get an icon by name from the resources directory.
 QPixmap GetIcon (QString iconName);
-
-// Asks the user a yes/no question with the given message and the given window title.
-// Returns true if the user answered yes, false if no.
-bool Confirm (const QString& title, const QString& message); // Generic confirm prompt
-
-// An overload of confirm(), this asks the user a yes/no question with the given message.
-// Returns true if the user answered yes, false if no.
-bool Confirm (const QString& message);
-
-// Displays an error prompt with the given message
-void Critical (const QString& message);
-void errorPrompt (QWidget *parent, const QString& message);
-
-// Takes in pairs of radio buttons and respective values and finds the first selected one.
-// Returns returns the value of the first found radio button that was checked by the user.
-template<class T>
-T RadioSwitch (const T& defval, QList<Pair<QRadioButton*, T>> haystack)
-{
-	for (Pair<QRadioButton*, const T&> i : haystack)
-	{
-		if (i.first->isChecked())
-			return i.second;
-	}
-
-	return defval;
-}
-
-// Takes in pairs of radio buttons and respective values and checks the first found radio button whose respsective value
-// matches expr have the given value.
-template<class T>
-void RadioDefault (const T& expr, QList<Pair<QRadioButton*, T>> haystack)
-{
-	for (Pair<QRadioButton*, const T&> i : haystack)
-	{
-		if (i.second == expr)
-		{
-			i.first->setChecked (true);
-			return;
-		}
-	}
-}
-
-QSettings* makeSettings (QObject* parent = nullptr);
+class QSettings* makeSettings(QObject* parent = nullptr);
--- a/src/partdownloader.cpp	Thu Feb 23 19:33:56 2017 +0200
+++ b/src/partdownloader.cpp	Thu Feb 23 19:56:21 2017 +0200
@@ -193,7 +193,9 @@
 		if (QFile::exists(downloadPath() + DIRSLASH + destination))
 		{
 			QString message = format(tr("%1 already exists in download directory. Overwrite?"), destination);
-			if (not Confirm(tr("Overwrite?"), message))
+			int answer = QMessageBox::question(this, tr("Overwrite"), message, (QMessageBox::Yes | QMessageBox::No), QMessageBox::No);
+
+			if (answer != QMessageBox::Yes)
 				return;
 		}
 
--- a/src/partdownloadrequest.cpp	Thu Feb 23 19:33:56 2017 +0200
+++ b/src/partdownloadrequest.cpp	Thu Feb 23 19:56:21 2017 +0200
@@ -17,6 +17,7 @@
  */
 
 #include <QDir>
+#include <QMessageBox>
 #include <QNetworkAccessManager>
 #include <QNetworkRequest>
 #include <QNetworkReply>
@@ -50,7 +51,7 @@
 		print ("Creating %1...\n", dirpath);
 
 		if (not dir.mkpath (dirpath))
-			Critical (format (tr ("Couldn't create the directory %1!"), dirpath));
+			QMessageBox::critical(m_window, tr("Error"), format(tr("Couldn't create the directory %1!"), dirpath));
 	}
 
 	m_networkReply = m_networkManager->get (QNetworkRequest (QUrl (url)));
@@ -179,7 +180,7 @@
 	if (networkReply()->error() != QNetworkReply::NoError)
 	{
 		if (isPrimary() and not prompt()->isAborted())
-			Critical (networkReply()->errorString());
+			QMessageBox::critical(m_window, tr("Error"), networkReply()->errorString());
 
 		print ("Unable to download %1: %2\n", destination(), networkReply()->errorString());
 		m_state = State::Failed;
@@ -262,7 +263,7 @@
 
 		if (not m_filePointer->open (QIODevice::WriteOnly))
 		{
-			Critical (format (tr ("Couldn't open %1 for writing: %2"), filePath(), strerror (errno)));
+			QMessageBox::critical(m_window, tr("Error"), format(tr("Couldn't open %1 for writing: %2"), filePath(), strerror(errno)));
 			m_state = State::Failed;
 			networkReply()->abort();
 			updateToTable();
@@ -282,4 +283,4 @@
 void PartDownloadRequest::abort()
 {
 	networkReply()->abort();
-}
\ No newline at end of file
+}
--- a/src/primitives.cpp	Thu Feb 23 19:33:56 2017 +0200
+++ b/src/primitives.cpp	Thu Feb 23 19:56:21 2017 +0200
@@ -17,6 +17,7 @@
  */
 
 #include <QApplication>
+#include <QMessageBox>
 #include "lddocument.h"
 #include "mainwindow.h"
 #include "primitives.h"
@@ -181,7 +182,8 @@
 
 	if (not categoriesFile.open (QIODevice::ReadOnly))
 	{
-		Critical(format(tr("Failed to open primitive categories: %1"), categoriesFile.errorString()));
+		QString message = format(tr("Failed to open primitive categories: %1"), categoriesFile.errorString());
+		QMessageBox::critical(m_window, tr("Cannot open categories"), message);
 		return;
 	}
 
@@ -635,7 +637,7 @@
 		}
 		else
 		{
-			errorPrompt(m_window, format("Couldn't write primitive list %1: %2", path, configFile.errorString()));
+			QMessageBox::critical(m_window, tr("Error"), format("Couldn't write primitive list %1: %2", path, configFile.errorString()));
 		}
 
 		emit workDone();
--- a/src/toolsets/algorithmtoolset.cpp	Thu Feb 23 19:33:56 2017 +0200
+++ b/src/toolsets/algorithmtoolset.cpp	Thu Feb 23 19:56:21 2017 +0200
@@ -17,11 +17,9 @@
  */
 
 #include <limits>
-#include <QBoxLayout>
-#include <QCheckBox>
 #include <QDir>
 #include <QInputDialog>
-#include <QSpinBox>
+#include <QMessageBox>
 #include "../mainwindow.h"
 #include "../main.h"
 #include "../lddocument.h"
@@ -476,13 +474,14 @@
 	{
 		QString desiredPath = subdirname + "/s";
 		QString title = tr ("Create subfile directory?");
-		QString text = format (tr ("The directory <b>%1</b> is suggested for "
-			"subfiles. This directory does not exist, create it?"), desiredPath);
+		QString text = format(tr("The directory <b>%1</b> is suggested for subfiles. "
+		                         "This directory does not exist, do you want to create it?"), desiredPath);
 
-		if (QDir (desiredPath).exists() or Confirm (title, text))
+		if (QDir(desiredPath).exists()
+		    or QMessageBox::question(m_window, title, text, (QMessageBox::Yes | QMessageBox::No), QMessageBox::No) == QMessageBox::Yes)
 		{
 			subdirname = desiredPath;
-			QDir().mkpath (subdirname);
+			QDir().mkpath(subdirname);
 		}
 		else
 			return;
--- a/src/toolsets/extprogramtoolset.cpp	Thu Feb 23 19:33:56 2017 +0200
+++ b/src/toolsets/extprogramtoolset.cpp	Thu Feb 23 19:56:21 2017 +0200
@@ -20,6 +20,7 @@
 #include <QTemporaryFile>
 #include <QDialog>
 #include <QDialogButtonBox>
+#include <QMessageBox>
 #include <QSpinBox>
 #include <QCheckBox>
 #include <QComboBox>
@@ -173,14 +174,15 @@
 
 // =============================================================================
 //
-void ExtProgramToolset::writeObjects (const LDObjectList& objects, QString fname)
+void ExtProgramToolset::writeObjects (const LDObjectList& objects, QString filename)
 {
 	// Write the input file
-	QFile f (fname);
+	QFile f (filename);
 
 	if (not f.open (QIODevice::WriteOnly | QIODevice::Text))
 	{
-		Critical (format ("Couldn't open temporary file %1 for writing: %2\n", fname, f.errorString()));
+		QString message = format(tr("Couldn't open temporary file %1 for writing: %2\n"), filename, f.errorString());
+		QMessageBox::critical(m_window, tr("Error"), message);
 		return;
 	}
 
@@ -188,7 +190,7 @@
 	f.close();
 
 #ifdef DEBUG
-	QFile::copy (fname, "debug_lastInput");
+	QFile::copy (filename, "debug_lastInput");
 #endif
 }
 
@@ -245,8 +247,8 @@
 
 	if (not process.waitForStarted())
 	{
-		Critical (format ("Couldn't start %1: %2\n", externalProgramName (program),
-			errorCodeString (program, process)));
+		QString message = format("Couldn't start %1: %2\n", externalProgramName(program), errorCodeString(program, process));
+		QMessageBox::critical(m_window, tr("Error running external program"), message);
 		return false;
 	}
 
@@ -256,18 +258,19 @@
 	// Wait while it runs
 	process.waitForFinished();
 
-	QString err = "";
+	QString errorMessage = "";
 
 	if (process.exitStatus() != QProcess::NormalExit)
-		err = errorCodeString (program, process);
+		errorMessage = errorCodeString (program, process);
 
 	// Check the return code
 	if (process.exitCode() != 0)
-		err = format ("Program exited abnormally (return code %1).",  process.exitCode());
+		errorMessage = format ("Program exited abnormally (return code %1).",  process.exitCode());
 
-	if (not err.isEmpty())
+	if (not errorMessage.isEmpty())
 	{
-		Critical (format ("%1 failed: %2\n", externalProgramName (program), err));
+		QString message = format(tr("%1 failed: %2\n"), externalProgramName(program), errorMessage);
+		QMessageBox::critical(m_window, tr("External program failed"), message);
 		QString filename ("externalProgramOutput.txt");
 		QFile file (filename);
 
@@ -302,7 +305,8 @@
 
 	if (not f.open (QIODevice::ReadOnly))
 	{
-		Critical (format ("Couldn't open temporary file %1 for reading.\n", fname));
+		QString message = format(tr("Couldn't open temporary file %1 for reading.\n"), fname);
+		QMessageBox::critical(m_window, tr("Error running external program"), message);
 		return;
 	}
 
@@ -454,22 +458,11 @@
 	LDColor inCol, cutCol;
 	const bool repeatInverse = ui.cb_repeat->isChecked();
 
-	forever
-	{
-		if (not dlg->exec())
-			return;
-
-		inCol = ui.cmb_incol->itemData (ui.cmb_incol->currentIndex()).toInt();
-		cutCol = ui.cmb_cutcol->itemData (ui.cmb_cutcol->currentIndex()).toInt();
+	if (not dlg->exec())
+		return;
 
-		if (inCol == cutCol)
-		{
-			Critical ("Cannot use the same color group for both input and cutter!");
-			continue;
-		}
-
-		break;
-	}
+	inCol = ui.cmb_incol->itemData (ui.cmb_incol->currentIndex()).toInt();
+	cutCol = ui.cmb_cutcol->itemData (ui.cmb_cutcol->currentIndex()).toInt();
 
 	// Five temporary files!
 	// indat = input group file
@@ -547,25 +540,11 @@
 	guiUtilities()->fillUsedColorsToComboBox (ui.cmb_col1);
 	guiUtilities()->fillUsedColorsToComboBox (ui.cmb_col2);
 
-	LDColor in1Col, in2Col;
-
-	forever
-	{
-		if (not dlg->exec())
-			return;
-
-		in1Col = ui.cmb_col1->itemData (ui.cmb_col1->currentIndex()).toInt();
-		in2Col = ui.cmb_col2->itemData (ui.cmb_col2->currentIndex()).toInt();
+	if (not dlg->exec())
+		return;
 
-		if (in1Col == in2Col)
-		{
-			Critical ("Cannot use the same color group for both inputs!");
-			continue;
-		}
-
-		break;
-	}
-
+	LDColor in1Col = ui.cmb_col1->itemData (ui.cmb_col1->currentIndex()).toInt();
+	LDColor in2Col = ui.cmb_col2->itemData (ui.cmb_col2->currentIndex()).toInt();
 	QTemporaryFile in1dat, in2dat, outdat;
 	QString in1DATName, in2DATName, outDATName;
 
@@ -614,23 +593,11 @@
 
 	LDColor in1Col, in2Col;
 
-	// Run the dialog and validate input
-	forever
-	{
-		if (not dlg->exec())
-			return;
-
-		in1Col = ui.cmb_col1->itemData (ui.cmb_col1->currentIndex()).toInt();
-		in2Col = ui.cmb_col2->itemData (ui.cmb_col2->currentIndex()).toInt();
+	if (not dlg->exec())
+		return;
 
-		if (in1Col == in2Col)
-		{
-			Critical ("Cannot use the same color group for both input and cutter!");
-			continue;
-		}
-
-		break;
-	}
+	in1Col = ui.cmb_col1->itemData (ui.cmb_col1->currentIndex()).toInt();
+	in2Col = ui.cmb_col2->itemData (ui.cmb_col2->currentIndex()).toInt();
 
 	QTemporaryFile in1dat, in2dat, outdat;
 	QString in1DATName, in2DATName, outDATName;
--- a/src/toolsets/filetoolset.cpp	Thu Feb 23 19:33:56 2017 +0200
+++ b/src/toolsets/filetoolset.cpp	Thu Feb 23 19:56:21 2017 +0200
@@ -133,7 +133,7 @@
 		}
 		else
 		{
-			Critical(format("Couldn't open %1 (%2)", filePath, file.errorString()));
+			QMessageBox::critical(m_window, tr("Error"), format(tr("Couldn't open %1 (%2)"), filePath, file.errorString()));
 		}
 	}
 }
@@ -162,7 +162,7 @@
 	}
 	else
 	{
-		Critical(format("Unable to open %1 for writing (%2)", filePath, file.errorString()));
+		QMessageBox::critical(m_window, tr("Error"), format(tr("Unable to open %1 for writing: %2"), filePath, file.errorString()));
 	}
 }
 
--- a/src/toolsets/viewtoolset.cpp	Thu Feb 23 19:33:56 2017 +0200
+++ b/src/toolsets/viewtoolset.cpp	Thu Feb 23 19:56:21 2017 +0200
@@ -18,6 +18,7 @@
 
 #include <QFileDialog>
 #include <QInputDialog>
+#include <QMessageBox>
 #include "../mainwindow.h"
 #include "../lddocument.h"
 #include "../miscallenous.h"
@@ -120,7 +121,10 @@
 	QString filename = QFileDialog::getSaveFileName (m_window, "Save Screencap", defaultname, imageformats);
 
 	if (not filename.isEmpty() and not image.save (filename))
-		Critical (format ("Couldn't open %1 for writing to save screencap: %2", filename, strerror (errno)));
+	{
+		QString message = format(tr("Couldn't open %1 for writing to save screen capture: %2"), filename, strerror(errno));
+		QMessageBox::critical(m_window, tr("Error"), message);
+	}
 }
 
 void ViewToolset::axes()

mercurial