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