vmessagebox.cpp

Go to the documentation of this file.
00001 /****************************************************************
00002  *  Vidalia is distributed under the following license:
00003  *
00004  *  Copyright (C) 2006,  Matt Edman, Justin Hipple
00005  *
00006  *  This program is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU General Public License
00008  *  as published by the Free Software Foundation; either version 2
00009  *  of the License, or (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, 
00019  *  Boston, MA  02110-1301, USA.
00020  ****************************************************************/
00021 
00022 /** 
00023  * \file vmessagebox.cpp
00024  * \version $Id: vmessagebox.cpp 1839 2007-08-21 04:33:14Z edmanm $
00025  * \brief Provides a custom Vidalia mesage box
00026  */
00027 
00028 #include <util/html.h>
00029 #include "vmessagebox.h"
00030 
00031 
00032 /** Default constructor. */
00033 VMessageBox::VMessageBox(QWidget *parent)
00034 : QMessageBox(parent)
00035 {
00036 }
00037 
00038 /** Returns the button (0, 1, or 2) that is OR-ed with QMessageBox::Default,
00039  * or 0 if none are. */
00040 int
00041 VMessageBox::defaultButton(int button0, int button1, int button2)
00042 {
00043   Q_UNUSED(button0);
00044   int defaultButton = 0;
00045   if (button1 & QMessageBox::Default) {
00046     defaultButton = 1;
00047   } else if (button2 & QMessageBox::Default) {
00048     defaultButton = 2;
00049   }
00050   return defaultButton;
00051 }
00052 
00053 /** Returns the button (0, 1, or 2) that is OR-ed with QMessageBox::Escape,
00054  * or -1 if none are. */
00055 int
00056 VMessageBox::escapeButton(int button0, int button1, int button2)
00057 {
00058   int escapeButton = -1;
00059   if (button0 & QMessageBox::Escape) {
00060     escapeButton = 0;
00061   } else if (button1 & QMessageBox::Escape) {
00062     escapeButton = 1;
00063   } else if (button2 & QMessageBox::Escape) {
00064     escapeButton = 2;
00065   }
00066   return escapeButton;
00067 }
00068 
00069 /** Returns the Button enum value from the given return value. */
00070 int
00071 VMessageBox::selected(int ret, int button0, int button1, int button2)
00072 {
00073   if (ret == 0) {
00074     return (button0 & QMessageBox::ButtonMask);
00075   } else if (ret == 1) {
00076     return (button1 & QMessageBox::ButtonMask);
00077   }
00078   return (button2 & QMessageBox::ButtonMask);
00079 }
00080 
00081 /** Converts a Button enum value to a translated string. */
00082 QString
00083 VMessageBox::buttonText(int btn)
00084 {
00085   QString text;
00086   int button = (btn & ~QMessageBox::FlagMask);
00087   switch (button) {
00088     case Ok:      text = tr("OK"); break;
00089     case Cancel:  text = tr("Cancel"); break;
00090     case Yes:     text = tr("Yes"); break;
00091     case No:      text = tr("No"); break;
00092     case Help:    text = tr("Help"); break;
00093     case Retry:   text = tr("Retry"); break;
00094     case ShowLog: text = tr("Show Log"); break;
00095     case ShowSettings: text = tr("Show Settings"); break;
00096     case Continue: text = tr("Continue"); break;
00097     case Quit:     text = tr("Quit"); break;
00098     case Browse:   text = tr("Browse"); break;
00099     default: break;
00100   }
00101   return text;
00102 }
00103 
00104 /** Displays a critical message box with the given caption, message text, and
00105  * visible buttons. To specify a button as a default button or an escape
00106  * button, OR the Button enum value with QMessageBox::Default or
00107  * QMessageBox::Escape, respectively. */
00108 int
00109 VMessageBox::critical(QWidget *parent, QString caption, QString text,
00110                       int button0, int button1, int button2)
00111 {
00112   int ret = QMessageBox::critical(parent, caption, p(text),
00113               VMessageBox::buttonText(button0), 
00114               VMessageBox::buttonText(button1), 
00115               VMessageBox::buttonText(button2),
00116               VMessageBox::defaultButton(button0, button1, button2), 
00117               VMessageBox::escapeButton(button0, button1, button2));
00118   return VMessageBox::selected(ret, button0, button1, button2);
00119 }
00120 
00121 /** Displays an question message box with the given caption, message text, and
00122  * visible buttons. To specify a button as a default button or an escape
00123  * button, OR the Button enum value with QMessageBox::Default or
00124  * QMessageBox::Escape, respectively. */
00125 int
00126 VMessageBox::question(QWidget *parent, QString caption, QString text,
00127                       int button0, int button1, int button2)
00128 {
00129   int ret = QMessageBox::question(parent, caption, p(text),
00130               VMessageBox::buttonText(button0), 
00131               VMessageBox::buttonText(button1), 
00132               VMessageBox::buttonText(button2),
00133               VMessageBox::defaultButton(button0, button1, button2), 
00134               VMessageBox::escapeButton(button0, button1, button2));
00135   return VMessageBox::selected(ret, button0, button1, button2);
00136 }
00137 
00138 /** Displays an information message box with the given caption, message text, and
00139  * visible buttons. To specify a button as a default button or an escape
00140  * button, OR the Button enum value with QMessageBox::Default or
00141  * QMessageBox::Escape, respectively. */
00142 int
00143 VMessageBox::information(QWidget *parent, QString caption, QString text,
00144                          int button0, int button1, int button2)
00145 {
00146   int ret = QMessageBox::information(parent, caption, p(text),
00147               VMessageBox::buttonText(button0), 
00148               VMessageBox::buttonText(button1), 
00149               VMessageBox::buttonText(button2),
00150               VMessageBox::defaultButton(button0, button1, button2), 
00151               VMessageBox::escapeButton(button0, button1, button2));
00152   return VMessageBox::selected(ret, button0, button1, button2);
00153 }
00154 
00155 /** Displays a warning message box with the given caption, message text, and
00156  * visible buttons. To specify a button as a default button or an escape
00157  * button, OR the Button enum value with QMessageBox::Default or
00158  * QMessageBox::Escape, respectively. */
00159 int
00160 VMessageBox::warning(QWidget *parent, QString caption, QString text,
00161                      int button0, int button1, int button2)
00162 {
00163   int ret = QMessageBox::warning(parent, caption, p(text),
00164               VMessageBox::buttonText(button0), 
00165               VMessageBox::buttonText(button1), 
00166               VMessageBox::buttonText(button2),
00167               VMessageBox::defaultButton(button0, button1, button2), 
00168               VMessageBox::escapeButton(button0, button1, button2));
00169   return VMessageBox::selected(ret, button0, button1, button2);
00170 }
00171 

Generated on Wed Sep 5 15:49:28 2007 for Vidalia by  doxygen 1.5.3