2006-03-05 17:24:44 +00:00
|
|
|
/**
|
2007-11-14 00:04:00 +00:00
|
|
|
* \file qt4/GuiAlert.cpp
|
2006-03-05 17:24:44 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author John Levon
|
2009-04-19 11:57:52 +00:00
|
|
|
* \author Jürgen Spitzmüller
|
2006-03-05 17:24:44 +00:00
|
|
|
* \author Abdelrazak Younes
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-04-28 20:44:46 +00:00
|
|
|
#include "alert.h"
|
2010-10-21 00:07:48 +00:00
|
|
|
#include "InGuiThread.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2007-01-23 12:38:20 +00:00
|
|
|
#include "frontends/Application.h"
|
|
|
|
|
2007-11-13 23:21:29 +00:00
|
|
|
#include "qt_helpers.h"
|
|
|
|
#include "LyX.h" // for lyx::use_gui
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2010-10-21 00:07:48 +00:00
|
|
|
#include "support/gettext.h"
|
2008-02-18 07:14:42 +00:00
|
|
|
#include "support/debug.h"
|
2007-11-13 23:21:29 +00:00
|
|
|
#include "support/docstring.h"
|
|
|
|
#include "support/lstrings.h"
|
2014-06-07 09:12:31 +00:00
|
|
|
#include "support/lassert.h"
|
2009-12-21 11:48:19 +00:00
|
|
|
#include "support/ProgressInterface.h"
|
2007-11-13 23:21:29 +00:00
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
#include <QApplication>
|
2009-04-19 11:57:52 +00:00
|
|
|
#include <QCheckBox>
|
2006-03-05 17:24:44 +00:00
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QInputDialog>
|
2010-04-19 20:44:57 +00:00
|
|
|
#include <QPushButton>
|
2009-04-19 11:57:52 +00:00
|
|
|
#include <QSettings>
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2007-11-27 22:19:36 +00:00
|
|
|
#include <iomanip>
|
2008-02-07 17:04:06 +00:00
|
|
|
#include <iostream>
|
2007-11-27 22:19:36 +00:00
|
|
|
|
2010-10-20 23:50:49 +00:00
|
|
|
|
|
|
|
// sync with GuiView.cpp
|
2010-10-21 00:24:57 +00:00
|
|
|
#define EXPORT_in_THREAD 1
|
2010-10-20 23:50:49 +00:00
|
|
|
|
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2007-12-12 18:57:56 +00:00
|
|
|
using namespace lyx::support;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
namespace lyx {
|
2007-11-14 00:04:00 +00:00
|
|
|
namespace frontend {
|
2006-10-21 00:16:43 +00:00
|
|
|
|
2007-11-13 23:00:36 +00:00
|
|
|
|
2009-12-21 11:48:19 +00:00
|
|
|
void noAppDialog(QString const & title, QString const & msg, QMessageBox::Icon mode)
|
2009-04-19 11:57:52 +00:00
|
|
|
{
|
2009-12-21 11:48:19 +00:00
|
|
|
int argc = 1;
|
2010-03-06 14:57:16 +00:00
|
|
|
const char *argv[] = { "lyx", 0 };
|
|
|
|
|
|
|
|
QApplication app(argc, (char**)argv);
|
2009-12-21 11:48:19 +00:00
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case QMessageBox::Information: QMessageBox::information(0, title, msg); break;
|
|
|
|
case QMessageBox::Warning: QMessageBox::warning(0, title, msg); break;
|
|
|
|
case QMessageBox::Critical: QMessageBox::critical(0, title, msg); break;
|
|
|
|
default: break;
|
|
|
|
}
|
2009-04-19 11:57:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-14 00:04:00 +00:00
|
|
|
namespace Alert {
|
|
|
|
|
2010-10-20 23:50:49 +00:00
|
|
|
|
|
|
|
int doPrompt(docstring const & title0, docstring const & question,
|
2007-11-14 00:04:00 +00:00
|
|
|
int default_button, int cancel_button,
|
2010-04-19 20:44:57 +00:00
|
|
|
docstring const & b1, docstring const & b2,
|
|
|
|
docstring const & b3, docstring const & b4)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2008-02-22 10:30:46 +00:00
|
|
|
//lyxerr << "PROMPT" << title0 << "FOCUS: " << qApp->focusWidget() << endl;
|
2007-11-14 00:04:00 +00:00
|
|
|
if (!use_gui || lyxerr.debugging()) {
|
2007-12-12 20:35:55 +00:00
|
|
|
lyxerr << title0 << '\n'
|
2007-11-14 00:04:00 +00:00
|
|
|
<< "----------------------------------------\n"
|
2007-12-12 20:35:55 +00:00
|
|
|
<< question << endl;
|
2007-11-14 00:04:00 +00:00
|
|
|
|
|
|
|
lyxerr << "Assuming answer is ";
|
|
|
|
switch (default_button) {
|
2007-12-12 20:35:55 +00:00
|
|
|
case 0: lyxerr << b1 << endl;
|
|
|
|
case 1: lyxerr << b2 << endl;
|
|
|
|
case 2: lyxerr << b3 << endl;
|
2010-04-19 20:44:57 +00:00
|
|
|
case 3: lyxerr << b4 << endl;
|
2007-11-14 00:04:00 +00:00
|
|
|
}
|
|
|
|
if (!use_gui)
|
|
|
|
return default_button;
|
|
|
|
}
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2007-11-14 00:04:00 +00:00
|
|
|
docstring const title = bformat(_("LyX: %1$s"), title0);
|
2006-10-24 15:01:07 +00:00
|
|
|
|
2012-07-01 07:27:11 +00:00
|
|
|
/// Long operation in progress prevents user from Ok-ing the error dialog
|
|
|
|
bool long_op = theApp()->longOperationStarted();
|
|
|
|
if (long_op)
|
|
|
|
theApp()->stopLongOperation();
|
|
|
|
|
2010-01-11 01:27:15 +00:00
|
|
|
// For some reason, sometimes Qt uses a hourglass or watch cursor when
|
2006-12-21 20:29:15 +00:00
|
|
|
// displaying the alert. Hence, we ask for the standard cursor shape.
|
2010-01-11 01:27:15 +00:00
|
|
|
qApp->setOverrideCursor(Qt::ArrowCursor);
|
2006-12-21 20:29:15 +00:00
|
|
|
|
2007-11-18 20:36:52 +00:00
|
|
|
// FIXME replace that with guiApp->currentView()
|
2008-02-22 10:30:46 +00:00
|
|
|
//LYXERR0("FOCUS: " << qApp->focusWidget());
|
2010-04-19 20:44:57 +00:00
|
|
|
QPushButton * b[4] = { 0, 0, 0, 0 };
|
|
|
|
QMessageBox msg_box(QMessageBox::Information,
|
2011-03-02 00:26:34 +00:00
|
|
|
toqstr(title), toqstr(question),
|
2010-04-19 20:44:57 +00:00
|
|
|
QMessageBox::NoButton, qApp->focusWidget());
|
|
|
|
b[0] = msg_box.addButton(b1.empty() ? "OK" : toqstr(b1),
|
|
|
|
QMessageBox::ActionRole);
|
|
|
|
if (!b2.empty())
|
|
|
|
b[1] = msg_box.addButton(toqstr(b2), QMessageBox::ActionRole);
|
|
|
|
if (!b3.empty())
|
|
|
|
b[2] = msg_box.addButton(toqstr(b3), QMessageBox::ActionRole);
|
|
|
|
if (!b4.empty())
|
|
|
|
b[3] = msg_box.addButton(toqstr(b4), QMessageBox::ActionRole);
|
|
|
|
msg_box.setDefaultButton(b[default_button]);
|
|
|
|
msg_box.setEscapeButton(static_cast<QAbstractButton *>(b[cancel_button]));
|
|
|
|
int res = msg_box.exec();
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2010-01-11 01:27:15 +00:00
|
|
|
qApp->restoreOverrideCursor();
|
|
|
|
|
2012-07-01 07:27:11 +00:00
|
|
|
if (long_op)
|
|
|
|
theApp()->startLongOperation();
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
// Qt bug: can return -1 on cancel or WM close, despite the docs.
|
|
|
|
if (res == -1)
|
|
|
|
res = cancel_button;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2010-10-20 23:50:49 +00:00
|
|
|
int prompt(docstring const & title0, docstring const & question,
|
|
|
|
int default_button, int cancel_button,
|
|
|
|
docstring const & b1, docstring const & b2,
|
|
|
|
docstring const & b3, docstring const & b4)
|
|
|
|
{
|
|
|
|
#ifdef EXPORT_in_THREAD
|
|
|
|
return InGuiThread<int>().call(&doPrompt,
|
|
|
|
#else
|
|
|
|
return doPrompt(
|
|
|
|
#endif
|
|
|
|
title0, question, default_button,
|
|
|
|
cancel_button, b1, b2, b3, b4);
|
|
|
|
}
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2010-10-20 23:50:49 +00:00
|
|
|
void doWarning(docstring const & title0, docstring const & message,
|
2009-04-19 11:57:52 +00:00
|
|
|
bool const & askshowagain)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2007-12-12 20:35:55 +00:00
|
|
|
lyxerr << "Warning: " << title0 << '\n'
|
2007-11-14 00:04:00 +00:00
|
|
|
<< "----------------------------------------\n"
|
2007-12-12 20:35:55 +00:00
|
|
|
<< message << endl;
|
2007-11-14 00:04:00 +00:00
|
|
|
|
|
|
|
if (!use_gui)
|
|
|
|
return;
|
|
|
|
|
|
|
|
docstring const title = bformat(_("LyX: %1$s"), title0);
|
2006-10-24 15:01:07 +00:00
|
|
|
|
2007-01-23 12:38:20 +00:00
|
|
|
if (theApp() == 0) {
|
2011-03-02 00:26:34 +00:00
|
|
|
noAppDialog(toqstr(title), toqstr(message), QMessageBox::Warning);
|
2007-01-23 12:38:20 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-12-21 11:48:19 +00:00
|
|
|
|
2014-03-09 15:40:13 +00:00
|
|
|
/// Long operation in progress prevents user from Ok-ing the error dialog
|
|
|
|
bool long_op = theApp()->longOperationStarted();
|
|
|
|
if (long_op)
|
|
|
|
theApp()->stopLongOperation();
|
|
|
|
|
2010-01-11 01:27:15 +00:00
|
|
|
// Don't use a hourglass cursor while displaying the alert
|
|
|
|
qApp->setOverrideCursor(Qt::ArrowCursor);
|
|
|
|
|
2009-12-21 11:48:19 +00:00
|
|
|
if (!askshowagain) {
|
|
|
|
ProgressInterface::instance()->warning(
|
2009-04-19 11:57:52 +00:00
|
|
|
toqstr(title),
|
2011-03-02 00:26:34 +00:00
|
|
|
toqstr(message));
|
2009-12-21 11:48:19 +00:00
|
|
|
} else {
|
|
|
|
ProgressInterface::instance()->toggleWarning(
|
|
|
|
toqstr(title),
|
|
|
|
toqstr(message),
|
2011-03-02 00:26:34 +00:00
|
|
|
toqstr(message));
|
2009-12-21 11:48:19 +00:00
|
|
|
}
|
2010-01-11 01:27:15 +00:00
|
|
|
|
|
|
|
qApp->restoreOverrideCursor();
|
2014-03-09 15:40:13 +00:00
|
|
|
|
|
|
|
if (long_op)
|
|
|
|
theApp()->startLongOperation();
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
2010-10-20 23:50:49 +00:00
|
|
|
void warning(docstring const & title0, docstring const & message,
|
|
|
|
bool const & askshowagain)
|
|
|
|
{
|
|
|
|
#ifdef EXPORT_in_THREAD
|
|
|
|
InGuiThread<void>().call(&doWarning,
|
|
|
|
#else
|
|
|
|
doWarning(
|
|
|
|
#endif
|
|
|
|
title0, message, askshowagain);
|
|
|
|
}
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2014-06-07 09:12:31 +00:00
|
|
|
void doError(docstring const & title0, docstring const & message, bool backtrace)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2007-12-12 20:35:55 +00:00
|
|
|
lyxerr << "Error: " << title0 << '\n'
|
2007-11-14 00:04:00 +00:00
|
|
|
<< "----------------------------------------\n"
|
2007-12-12 20:35:55 +00:00
|
|
|
<< message << endl;
|
2007-11-14 00:04:00 +00:00
|
|
|
|
2014-06-07 09:12:31 +00:00
|
|
|
QString details;
|
|
|
|
if (backtrace) {
|
|
|
|
details = QString::fromLocal8Bit(to_local8bit(printCallStack()).c_str());
|
|
|
|
}
|
|
|
|
|
2007-11-14 00:04:00 +00:00
|
|
|
if (!use_gui)
|
|
|
|
return;
|
|
|
|
|
|
|
|
docstring const title = bformat(_("LyX: %1$s"), title0);
|
2009-12-21 11:48:19 +00:00
|
|
|
|
2007-01-23 12:38:20 +00:00
|
|
|
if (theApp() == 0) {
|
2011-03-02 00:26:34 +00:00
|
|
|
noAppDialog(toqstr(title), toqstr(message), QMessageBox::Critical);
|
2007-01-23 12:38:20 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-12-21 11:48:19 +00:00
|
|
|
|
2012-07-01 07:27:11 +00:00
|
|
|
/// Long operation in progress prevents user from Ok-ing the error dialog
|
|
|
|
bool long_op = theApp()->longOperationStarted();
|
|
|
|
if (long_op)
|
|
|
|
theApp()->stopLongOperation();
|
|
|
|
|
2010-01-11 01:27:15 +00:00
|
|
|
// Don't use a hourglass cursor while displaying the alert
|
|
|
|
qApp->setOverrideCursor(Qt::ArrowCursor);
|
|
|
|
|
2009-12-21 11:48:19 +00:00
|
|
|
ProgressInterface::instance()->error(
|
|
|
|
toqstr(title),
|
2014-06-07 09:12:31 +00:00
|
|
|
toqstr(message),
|
|
|
|
details);
|
2010-01-11 01:27:15 +00:00
|
|
|
|
|
|
|
qApp->restoreOverrideCursor();
|
2012-07-01 07:27:11 +00:00
|
|
|
|
|
|
|
if (long_op)
|
|
|
|
theApp()->startLongOperation();
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
2014-06-07 09:12:31 +00:00
|
|
|
void error(docstring const & title0, docstring const & message, bool backtrace)
|
2010-10-20 23:50:49 +00:00
|
|
|
{
|
|
|
|
#ifdef EXPORT_in_THREAD
|
|
|
|
InGuiThread<void>().call(&doError,
|
|
|
|
#else
|
|
|
|
doError(
|
|
|
|
#endif
|
2014-06-07 09:12:31 +00:00
|
|
|
title0, message, backtrace);
|
2010-10-20 23:50:49 +00:00
|
|
|
}
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2010-10-20 23:50:49 +00:00
|
|
|
void doInformation(docstring const & title0, docstring const & message)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2007-11-14 00:04:00 +00:00
|
|
|
if (!use_gui || lyxerr.debugging())
|
2007-12-12 20:35:55 +00:00
|
|
|
lyxerr << title0 << '\n'
|
2007-11-14 00:04:00 +00:00
|
|
|
<< "----------------------------------------\n"
|
2007-12-12 20:35:55 +00:00
|
|
|
<< message << endl;
|
2007-11-14 00:04:00 +00:00
|
|
|
|
|
|
|
if (!use_gui)
|
|
|
|
return;
|
|
|
|
|
|
|
|
docstring const title = bformat(_("LyX: %1$s"), title0);
|
2009-12-21 11:48:19 +00:00
|
|
|
|
|
|
|
if (theApp() == 0) {
|
2011-03-02 00:26:34 +00:00
|
|
|
noAppDialog(toqstr(title), toqstr(message), QMessageBox::Information);
|
2009-12-21 11:48:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-09 15:40:13 +00:00
|
|
|
/// Long operation in progress prevents user from Ok-ing the error dialog
|
|
|
|
bool long_op = theApp()->longOperationStarted();
|
|
|
|
if (long_op)
|
|
|
|
theApp()->stopLongOperation();
|
|
|
|
|
2010-01-11 01:27:15 +00:00
|
|
|
// Don't use a hourglass cursor while displaying the alert
|
|
|
|
qApp->setOverrideCursor(Qt::ArrowCursor);
|
|
|
|
|
2009-12-21 11:48:19 +00:00
|
|
|
ProgressInterface::instance()->information(
|
|
|
|
toqstr(title),
|
2011-03-02 00:26:34 +00:00
|
|
|
toqstr(message));
|
2010-01-11 01:27:15 +00:00
|
|
|
|
|
|
|
qApp->restoreOverrideCursor();
|
2014-03-09 15:40:13 +00:00
|
|
|
|
|
|
|
if (long_op)
|
|
|
|
theApp()->startLongOperation();
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
2010-10-20 23:50:49 +00:00
|
|
|
void information(docstring const & title0, docstring const & message)
|
|
|
|
{
|
|
|
|
#ifdef EXPORT_in_THREAD
|
|
|
|
InGuiThread<void>().call(&doInformation,
|
|
|
|
#else
|
|
|
|
doInformation(
|
|
|
|
#endif
|
|
|
|
title0, message);
|
|
|
|
}
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2010-10-20 23:50:49 +00:00
|
|
|
bool doAskForText(docstring & response, docstring const & msg,
|
2007-11-13 23:00:36 +00:00
|
|
|
docstring const & dflt)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2007-11-14 00:04:00 +00:00
|
|
|
if (!use_gui || lyxerr.debugging()) {
|
|
|
|
lyxerr << "----------------------------------------\n"
|
2007-12-12 20:35:55 +00:00
|
|
|
<< msg << '\n'
|
|
|
|
<< "Assuming answer is " << dflt << '\n'
|
2007-11-14 00:04:00 +00:00
|
|
|
<< "----------------------------------------" << endl;
|
|
|
|
if (!use_gui) {
|
|
|
|
response = dflt;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-11 08:54:10 +00:00
|
|
|
docstring const title = bformat(_("LyX: %1$s"), msg);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2014-03-09 15:40:13 +00:00
|
|
|
/// Long operation in progress prevents user from Ok-ing the error dialog
|
|
|
|
bool long_op = theApp()->longOperationStarted();
|
|
|
|
if (long_op)
|
|
|
|
theApp()->stopLongOperation();
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
bool ok;
|
2006-08-17 08:37:26 +00:00
|
|
|
QString text = QInputDialog::getText(qApp->focusWidget(),
|
2006-03-05 17:24:44 +00:00
|
|
|
toqstr(title),
|
2007-04-25 16:39:21 +00:00
|
|
|
toqstr(char_type('&') + msg),
|
2006-03-05 17:24:44 +00:00
|
|
|
QLineEdit::Normal,
|
|
|
|
toqstr(dflt), &ok);
|
|
|
|
|
2014-03-09 15:40:13 +00:00
|
|
|
if (long_op)
|
|
|
|
theApp()->startLongOperation();
|
|
|
|
|
2008-10-30 13:47:41 +00:00
|
|
|
if (ok) {
|
2007-11-13 23:00:36 +00:00
|
|
|
response = qstring_to_ucs4(text);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
response.clear();
|
|
|
|
return false;
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
2010-10-20 23:50:49 +00:00
|
|
|
bool askForText(docstring & response, docstring const & msg,
|
|
|
|
docstring const & dflt)
|
|
|
|
{
|
|
|
|
#ifdef EXPORT_in_THREAD
|
|
|
|
return InGuiThread<bool>().call(&doAskForText,
|
|
|
|
#else
|
2010-10-22 17:08:57 +00:00
|
|
|
return doAskForText(
|
2010-10-20 23:50:49 +00:00
|
|
|
#endif
|
|
|
|
response, msg, dflt);
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
2007-11-13 23:21:29 +00:00
|
|
|
} // namespace Alert
|
2007-11-14 00:04:00 +00:00
|
|
|
} // namespace frontend
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|