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 1238 2006-09-25 17:50:57Z 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;
00075   } else if (ret == 1) {
00076     return button1;
00077   }
00078   return button2;
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     default: break;
00097   }
00098   return text;
00099 }
00100 
00101 /** Displays a critical message box with the given caption, message text, and
00102  * visible buttons. To specify a button as a default button or an escape
00103  * button, OR the Button enum value with QMessageBox::Default or
00104  * QMessageBox::Escape, respectively. */
00105 int
00106 VMessageBox::critical(QWidget *parent, QString caption, QString text,
00107                       int button0, int button1, int button2)
00108 {
00109   int ret = QMessageBox::critical(parent, caption, p(text),
00110               VMessageBox::buttonText(button0), 
00111               VMessageBox::buttonText(button1), 
00112               VMessageBox::buttonText(button2),
00113               VMessageBox::defaultButton(button0, button1, button2), 
00114               VMessageBox::escapeButton(button0, button1, button2));
00115   return VMessageBox::selected(ret, button0, button1, button2);
00116 }
00117 
00118 /** Displays an question message box with the given caption, message text, and
00119  * visible buttons. To specify a button as a default button or an escape
00120  * button, OR the Button enum value with QMessageBox::Default or
00121  * QMessageBox::Escape, respectively. */
00122 int
00123 VMessageBox::question(QWidget *parent, QString caption, QString text,
00124                       int button0, int button1, int button2)
00125 {
00126   int ret = QMessageBox::question(parent, caption, p(text),
00127               VMessageBox::buttonText(button0), 
00128               VMessageBox::buttonText(button1), 
00129               VMessageBox::buttonText(button2),
00130               VMessageBox::defaultButton(button0, button1, button2), 
00131               VMessageBox::escapeButton(button0, button1, button2));
00132   return VMessageBox::selected(ret, button0, button1, button2);
00133 }
00134 
00135 /** Displays an information message box with the given caption, message text, and
00136  * visible buttons. To specify a button as a default button or an escape
00137  * button, OR the Button enum value with QMessageBox::Default or
00138  * QMessageBox::Escape, respectively. */
00139 int
00140 VMessageBox::information(QWidget *parent, QString caption, QString text,
00141                          int button0, int button1, int button2)
00142 {
00143   int ret = QMessageBox::information(parent, caption, p(text),
00144               VMessageBox::buttonText(button0), 
00145               VMessageBox::buttonText(button1), 
00146               VMessageBox::buttonText(button2),
00147               VMessageBox::defaultButton(button0, button1, button2), 
00148               VMessageBox::escapeButton(button0, button1, button2));
00149   return VMessageBox::selected(ret, button0, button1, button2);
00150 }
00151 
00152 /** Displays a warning message box with the given caption, message text, and
00153  * visible buttons. To specify a button as a default button or an escape
00154  * button, OR the Button enum value with QMessageBox::Default or
00155  * QMessageBox::Escape, respectively. */
00156 int
00157 VMessageBox::warning(QWidget *parent, QString caption, QString text,
00158                      int button0, int button1, int button2)
00159 {
00160   int ret = QMessageBox::warning(parent, caption, p(text),
00161               VMessageBox::buttonText(button0), 
00162               VMessageBox::buttonText(button1), 
00163               VMessageBox::buttonText(button2),
00164               VMessageBox::defaultButton(button0, button1, button2), 
00165               VMessageBox::escapeButton(button0, button1, button2));
00166   return VMessageBox::selected(ret, button0, button1, button2);
00167 }
00168 

Generated on Mon Oct 23 20:08:16 2006 for Vidalia by  doxygen 1.5.0