diff options
author | Gerald Combs <gerald@wireshark.org> | 2014-09-24 14:06:23 -0700 |
---|---|---|
committer | Gerald Combs <gerald@wireshark.org> | 2014-09-25 22:02:43 +0000 |
commit | ea6fa049c9c43a6852c2a1e8b72598e13a27786b (patch) | |
tree | 317fec804ce7aaaf619f813bba9c0efda22020b6 /ui/qt/simple_dialog.cpp | |
parent | 2ee45fe2daffe10b519dc54699480db1c0a4f5ed (diff) |
Update the simple dialog code.
Rename simple_dialog_qt.{cpp,h} to simple_dialog.{cpp,h}. Make it a
subclass of QMessageBox. Queue messages at startup similar to GTK+.
Move the GTK+-specific simple_dialog declarations to
gtk/simple_dialog.h.
Don't yell at the user so much. Replace exclamation points with periods.
Change-Id: I1cc771106222d5e06f1f52d67ac29d6dc367cce4
Reviewed-on: https://code.wireshark.org/review/4288
Reviewed-by: Gerald Combs <gerald@wireshark.org>
Diffstat (limited to 'ui/qt/simple_dialog.cpp')
-rw-r--r-- | ui/qt/simple_dialog.cpp | 266 |
1 files changed, 266 insertions, 0 deletions
diff --git a/ui/qt/simple_dialog.cpp b/ui/qt/simple_dialog.cpp new file mode 100644 index 0000000000..9f6335fd20 --- /dev/null +++ b/ui/qt/simple_dialog.cpp @@ -0,0 +1,266 @@ +/* simple_dialog.cpp + * + * Wireshark - Network traffic analyzer + * By Gerald Combs <gerald@wireshark.org> + * Copyright 1998 Gerald Combs + * + * 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 "simple_dialog.h" + +#include "epan/strutil.h" + +#include "ui/utf8_entities.h" + +#include "qt_ui_utils.h" + +#include <QMessageBox> +#include <QTextCodec> + +/* Simple dialog function - Displays a dialog box with the supplied message + * text. + * + * This is meant to be used as a backend for the functions defined in + * ui/simple_dialog.h. Qt code should use QMessageBox directly. + * + * Args: + * type : One of ESD_TYPE_*. + * btn_mask : The value passed in determines which buttons are displayed. + * msg_format : Sprintf-style format of the text displayed in the dialog. + * ... : Argument list for msg_format + */ + +QList<MessagePair> message_queue_; +ESD_TYPE_E max_severity_ = ESD_TYPE_INFO; + +const char *primary_delimiter_ = "\uffff"; + +const char * +simple_dialog_primary_start(void) { + return primary_delimiter_; +} + +const char * +simple_dialog_primary_end(void) { + return primary_delimiter_; +} + +char * +simple_dialog_format_message(const char *msg) +{ + char *str; + + if (msg) { + str = xml_escape(msg); + } else { + str = NULL; + } + return str; +} + +/* + * Error alert box, taking a format and a list of arguments. + */ +void +simple_error_message_box(const char *msg_format, ...) +{ + va_list ap; + + va_start(ap, msg_format); + vsimple_error_message_box(msg_format, ap); + va_end(ap); +} + +#include <QDebug> +SimpleDialog::SimpleDialog(QWidget *parent, ESD_TYPE_E type, int btn_mask, const char *msg_format, va_list ap) : + QMessageBox(parent) +{ + gchar *vmessage; + QString message; + + vmessage = g_strdup_vprintf(msg_format, ap); + message = QTextCodec::codecForLocale()->toUnicode(vmessage); + g_free(vmessage); + + MessagePair msg_pair = splitMessage(message); + QString primary = msg_pair.first; + QString secondary = msg_pair.second; + + if (primary.isEmpty()) { + return; + } + + if (!parent) { + message_queue_ << msg_pair; + if (type > max_severity_) { + max_severity_ = type; + } + return; + } + + switch(type) { + case ESD_TYPE_ERROR: + setIcon(QMessageBox::Critical); + break; + case ESD_TYPE_WARN: + setIcon(QMessageBox::Warning); + break; + case ESD_TYPE_CONFIRMATION: + setIcon(QMessageBox::Question); + break; + case ESD_TYPE_INFO: + default: + setIcon(QMessageBox::Information); + break; + } + + if (btn_mask & ESD_BTN_OK) { + addButton(QMessageBox::Ok); + } + if (btn_mask & ESD_BTN_CANCEL) { + addButton(QMessageBox::Cancel); + } + if (btn_mask & ESD_BTN_YES) { + addButton(QMessageBox::Yes); + } + if (btn_mask & ESD_BTN_NO) { + addButton(QMessageBox::No); + } +// if (btn_mask & ESD_BTN_CLEAR) { +// addButton(QMessageBox::); +// } + if (btn_mask & ESD_BTN_SAVE) { + addButton(QMessageBox::Save); + } + if (btn_mask & ESD_BTN_DONT_SAVE) { + addButton(QMessageBox::Discard); + } +// if (btn_mask & ESD_BTN_QUIT_DONT_SAVE) { +// addButton(QMessageBox::); +// } + + setText(primary); + setInformativeText(secondary); +} + +SimpleDialog::~SimpleDialog() +{ +} + +void SimpleDialog::displayQueuedMessages(QWidget *parent) +{ + if (message_queue_.isEmpty()) { + return; + } + + QMessageBox mb(parent); + + switch(max_severity_) { + case ESD_TYPE_ERROR: + mb.setIcon(QMessageBox::Critical); + break; + case ESD_TYPE_WARN: + mb.setIcon(QMessageBox::Warning); + break; + case ESD_TYPE_CONFIRMATION: + mb.setIcon(QMessageBox::Question); + break; + case ESD_TYPE_INFO: + default: + mb.setIcon(QMessageBox::Information); + break; + } + + mb.addButton(QMessageBox::Ok); + + if (message_queue_.length() > 1) { + QStringList msg_details; + QString first_primary = message_queue_[0].first; + first_primary.append(UTF8_HORIZONTAL_ELLIPSIS); + + mb.setText(tr("Multiple problems found")); + mb.setInformativeText(first_primary); + + foreach (MessagePair msg_pair, message_queue_) { + msg_details << msg_pair.first; + if (!msg_pair.second.isEmpty()) { + msg_details.append(msg_pair.second); + } + } + mb.setDetailedText(msg_details.join("\n\n")); + } else { + mb.setText(message_queue_[0].first); + mb.setInformativeText(message_queue_[0].second); + } + + message_queue_.clear(); + max_severity_ = ESD_TYPE_INFO; + + mb.exec(); +} + +int SimpleDialog::exec() +{ + if (!parentWidget()) { + return 0; + } + + switch (QMessageBox::exec()) { + case QMessageBox::Ok: + return ESD_BTN_OK; + case QMessageBox::Yes: + return ESD_BTN_YES; + case QMessageBox::No: + return ESD_BTN_NO; + case QMessageBox::Save: + return ESD_BTN_SAVE; + case QMessageBox::Discard: + return ESD_BTN_DONT_SAVE; + case QMessageBox::Cancel: // XXX Should OK be the default? + default: + return ESD_BTN_CANCEL; + } +} + +const MessagePair SimpleDialog::splitMessage(QString &message) const +{ + if (message.startsWith(primary_delimiter_)) { + QStringList parts = message.split(primary_delimiter_, QString::SkipEmptyParts); + switch (parts.length()) { + case 0: + return MessagePair(QString(), QString()); + case 1: + return MessagePair(parts[0], QString()); + default: + QString first = parts.takeFirst(); + return MessagePair(first, parts.join(" ")); + } + } + return MessagePair(message, QString()); +} + +/* + * Editor modelines + * + * Local Variables: + * c-basic-offset: 4 + * tab-width: 8 + * indent-tabs-mode: nil + * End: + * + * ex: set shiftwidth=4 tabstop=8 expandtab: + * :indentSize=4:tabSize=8:noTabs=true: + */ |