Vidalia 0.2.15
VMessageBox.cpp
Go to the documentation of this file.
00001 /*
00002 **  This file is part of Vidalia, and is subject to the license terms in the
00003 **  LICENSE file, found in the top level directory of this distribution. If you
00004 **  did not receive the LICENSE file with this file, you may obtain it from the
00005 **  Vidalia source package distributed by the Vidalia Project at
00006 **  http://www.torproject.org/projects/vidalia.html. No part of Vidalia, 
00007 **  including this file, may be copied, modified, propagated, or distributed 
00008 **  except according to the terms described in the LICENSE file.
00009 */
00010 
00011 /*
00012 ** \file VMessageBox.cpp
00013 ** \brief Provides a custom Vidalia mesage box
00014 */
00015 
00016 #include "VMessageBox.h"
00017 
00018 #include "html.h"
00019 
00020 
00021 /** Default constructor. */
00022 VMessageBox::VMessageBox(QWidget *parent)
00023   : QMessageBox(parent)
00024 {
00025 }
00026 
00027 /** Returns the button (0, 1, or 2) that is OR-ed with QMessageBox::Default,
00028  * or 0 if none are. */
00029 int
00030 VMessageBox::defaultButton(int button0, int button1, int button2)
00031 {
00032   Q_UNUSED(button0);
00033   int defaultButton = 0;
00034   if (button1 & QMessageBox::Default) {
00035     defaultButton = 1;
00036   } else if (button2 & QMessageBox::Default) {
00037     defaultButton = 2;
00038   }
00039   return defaultButton;
00040 }
00041 
00042 /** Returns the button (0, 1, or 2) that is OR-ed with QMessageBox::Escape,
00043  * or -1 if none are. */
00044 int
00045 VMessageBox::escapeButton(int button0, int button1, int button2)
00046 {
00047   int escapeButton = -1;
00048   if (button0 & QMessageBox::Escape) {
00049     escapeButton = 0;
00050   } else if (button1 & QMessageBox::Escape) {
00051     escapeButton = 1;
00052   } else if (button2 & QMessageBox::Escape) {
00053     escapeButton = 2;
00054   }
00055   return escapeButton;
00056 }
00057 
00058 /** Returns the Button enum value from the given return value. */
00059 int
00060 VMessageBox::selected(int ret, int button0, int button1, int button2)
00061 {
00062   if (ret == 0) {
00063     return (button0 & QMessageBox::ButtonMask);
00064   } else if (ret == 1) {
00065     return (button1 & QMessageBox::ButtonMask);
00066   }
00067   return (button2 & QMessageBox::ButtonMask);
00068 }
00069 
00070 /** Converts a Button enum value to a translated string. */
00071 QString
00072 VMessageBox::buttonText(int btn)
00073 {
00074   QString text;
00075   int button = (btn & ~QMessageBox::FlagMask);
00076   switch (button) {
00077     case Ok:      text = tr("OK"); break;
00078     case Cancel:  text = tr("Cancel"); break;
00079     case Yes:     text = tr("Yes"); break;
00080     case No:      text = tr("No"); break;
00081     case Help:    text = tr("Help"); break;
00082     case Retry:   text = tr("Retry"); break;
00083     case ShowLog: text = tr("Show Log"); break;
00084     case ShowSettings: text = tr("Show Settings"); break;
00085     case Continue: text = tr("Continue"); break;
00086     case Quit:     text = tr("Quit"); break;
00087     case Browse:   text = tr("Browse"); break;
00088     default: break;
00089   }
00090   return text;
00091 }
00092 
00093 /** Displays a critical message box with the given caption, message text, and
00094  * visible buttons. To specify a button as a default button or an escape
00095  * button, OR the Button enum value with QMessageBox::Default or
00096  * QMessageBox::Escape, respectively. */
00097 int
00098 VMessageBox::critical(QWidget *parent, QString caption, QString text,
00099                       int button0, int button1, int button2)
00100 {
00101   int ret = QMessageBox::critical(parent, caption, p(text),
00102               VMessageBox::buttonText(button0), 
00103               VMessageBox::buttonText(button1), 
00104               VMessageBox::buttonText(button2),
00105               VMessageBox::defaultButton(button0, button1, button2), 
00106               VMessageBox::escapeButton(button0, button1, button2));
00107   return VMessageBox::selected(ret, button0, button1, button2);
00108 }
00109 
00110 /** Displays an question message box with the given caption, message text, and
00111  * visible buttons. To specify a button as a default button or an escape
00112  * button, OR the Button enum value with QMessageBox::Default or
00113  * QMessageBox::Escape, respectively. */
00114 int
00115 VMessageBox::question(QWidget *parent, QString caption, QString text,
00116                       int button0, int button1, int button2)
00117 {
00118   int ret = QMessageBox::question(parent, caption, p(text),
00119               VMessageBox::buttonText(button0), 
00120               VMessageBox::buttonText(button1), 
00121               VMessageBox::buttonText(button2),
00122               VMessageBox::defaultButton(button0, button1, button2), 
00123               VMessageBox::escapeButton(button0, button1, button2));
00124   return VMessageBox::selected(ret, button0, button1, button2);
00125 }
00126 
00127 /** Displays an information message box with the given caption, message text, and
00128  * visible buttons. To specify a button as a default button or an escape
00129  * button, OR the Button enum value with QMessageBox::Default or
00130  * QMessageBox::Escape, respectively. */
00131 int
00132 VMessageBox::information(QWidget *parent, QString caption, QString text,
00133                          int button0, int button1, int button2)
00134 {
00135   int ret = QMessageBox::information(parent, caption, p(text),
00136               VMessageBox::buttonText(button0), 
00137               VMessageBox::buttonText(button1), 
00138               VMessageBox::buttonText(button2),
00139               VMessageBox::defaultButton(button0, button1, button2), 
00140               VMessageBox::escapeButton(button0, button1, button2));
00141   return VMessageBox::selected(ret, button0, button1, button2);
00142 }
00143 
00144 /** Displays a warning message box with the given caption, message text, and
00145  * visible buttons. To specify a button as a default button or an escape
00146  * button, OR the Button enum value with QMessageBox::Default or
00147  * QMessageBox::Escape, respectively. */
00148 int
00149 VMessageBox::warning(QWidget *parent, QString caption, QString text,
00150                      int button0, int button1, int button2)
00151 {
00152   int ret = QMessageBox::warning(parent, caption, p(text),
00153               VMessageBox::buttonText(button0), 
00154               VMessageBox::buttonText(button1), 
00155               VMessageBox::buttonText(button2),
00156               VMessageBox::defaultButton(button0, button1, button2), 
00157               VMessageBox::escapeButton(button0, button1, button2));
00158   return VMessageBox::selected(ret, button0, button1, button2);
00159 }
00160