QGpCoreTools/Message.h
Go to the documentation of this file.
00001 /***************************************************************************
00002 **
00003 **  This file is part of QGpCoreTools.
00004 **
00005 **  This library is free software; you can redistribute it and/or
00006 **  modify it under the terms of the GNU Lesser General Public
00007 **  License as published by the Free Software Foundation; either
00008 **  version 2.1 of the License, or (at your option) any later version.
00009 **
00010 **  This file is distributed in the hope that it will be useful, but WITHOUT
00011 **  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 **  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00013 **  License for more details.
00014 **
00015 **  You should have received a copy of the GNU Lesser General Public
00016 **  License along with this library; if not, write to the Free Software
00017 **  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018 **
00019 **  See http://www.geopsy.org for more information.
00020 **
00021 **  Created : 2005-10-04
00022 **  Authors :
00023 **    Marc Wathelet
00024 **    Marc Wathelet (LGIT, Grenoble, France)
00025 **
00026 ***************************************************************************/
00027 
00028 #ifndef MESSAGE_H
00029 #define MESSAGE_H
00030 
00031 #include "Translations.h"
00032 #include "QGpCoreToolsDLLExport.h"
00033 #include "MessageContext.h"
00034 
00035 namespace QGpCoreTools {
00036 
00037 #define MSG_ID Message::messageId(__FILE__, __LINE__)
00038 
00039 class QGPCORETOOLS_EXPORT Message
00040 {
00041   TRANSLATIONS("Message")
00042 public:
00043   Message();
00044   virtual ~Message() {}
00045 
00046   void beginContext(bool quiet=false);
00047   void endContext();
00048 
00049   static uint messageId(const char * fileName, int lineNumber);
00050 
00051   enum Answer {Answer0, Answer1, Answer2};
00052   enum Severity {Question, Information, Warning, Critical, Fatal};
00053 
00054   static void wrongTextFormat(FILE * fErr, const QString& caption, const QString& fileName);
00055   static void wrongTextFormat(QTextStream& sErr, const QString& caption);
00056   static QString getOpenFileName(const QString & caption,
00057                                   const QString & filter=QString::null,
00058                                   const QString & directory=QString::null);
00059 
00060   static QString getSaveFileName(const QString & caption,
00061                                   const QString & filter=QString::null,
00062                                   const QString & selection=QString::null);
00063 
00064   static QStringList getOpenFileNames(const QString & caption,
00065                                        const QString & filter=QString::null,
00066                                        const QString & directory=QString::null);
00067 
00068   static QString getExistingDirectory(const QString & caption,
00069                                        const QString & dir=QString::null);
00070 
00071   static inline Answer information(uint msgId, const QString & caption, const QString & text,
00072                                     bool againOption=false);
00073   static inline Answer information(uint msgId, const QString & caption, const QString & text,
00074                                     QString answer0, bool againOption=false);
00075   static inline Answer information(uint msgId, const QString & caption, const QString & text,
00076                                     QString answer0, QString answer1, bool againOption=false);
00077   static Answer information(uint msgId, const QString & caption, const QString & text,
00078                              QString answer0, QString answer1, QString answer2, bool againOption=false);
00079 
00080   static inline Answer warning(uint msgId, const QString & caption, const QString & text,
00081                                 bool againOption=false);
00082   static inline Answer warning(uint msgId, const QString & caption, const QString & text,
00083                                 QString answer0, bool againOption=false);
00084   static inline Answer warning(uint msgId, const QString & caption, const QString & text,
00085                                 QString answer0, QString answer1, bool againOption=false);
00086   static Answer warning(uint msgId, const QString & caption, const QString & text,
00087                          QString answer0, QString answer1, QString answer2, bool againOption=false);
00088 
00089   static inline Answer critical(uint msgId, const QString & caption, const QString & text,
00090                                  bool againOption=false);
00091   static inline Answer critical(uint msgId, const QString & caption, const QString & text,
00092                                  QString answer0, bool againOption=false);
00093   static inline Answer critical(uint msgId, const QString & caption, const QString & text,
00094                                  QString answer0, QString answer1, bool againOption=false);
00095   static Answer critical(uint msgId, const QString & caption, const QString & text,
00096                           QString answer0, QString answer1, QString answer2, bool againOption=false);
00097 
00098   static inline Answer question(uint msgId, const QString & caption, const QString & text,
00099                                  bool againOption=false);
00100   static inline Answer question(uint msgId, const QString & caption, const QString & text,
00101                                  QString answer0, bool againOption=false);
00102   static inline Answer question(uint msgId, const QString & caption, const QString & text,
00103                                  QString answer0, QString answer1, bool againOption=false);
00104   static Answer question(uint msgId, const QString & caption, const QString & text,
00105                           QString answer0, QString answer1, QString answer2, bool againOption=false);
00106 
00107   static QString ok() {return tr("OK");}
00108   static QString cancel() {return tr("Cancel");}
00109   static QString yes() {return tr("Yes");}
00110   static QString no() {return tr("No");}
00111   static QString close() {return tr("Close");}
00112   static QString abort() {return tr("Abort");}
00113   static QString retry() {return tr("Retry");}
00114   static QString ignore() {return tr("Ignore");}
00115   static QString yesAll() {return tr("Yes to all");}
00116   static QString noAll() {return tr("No to all");}
00117   static QString severityString(Severity sev);
00118 protected:
00119   inline bool hasAutoAnswer(uint msgId) const;
00120   inline bool quiet() const;
00121   void setAutoAnswer(uint msgId, Answer a);
00122   Answer autoAnswer(uint msgId,
00123                      Severity sev,
00124                      QString caption,
00125                      const QString & text,
00126                      QString answer0,
00127                      QString answer1,
00128                      QString answer2);
00129   virtual Answer message(uint msgId,
00130                           Severity sev,
00131                           QString caption,
00132                           const QString & text,
00133                           QString answer0,
00134                           QString answer1,
00135                           QString answer2,
00136                           bool againOption);
00137   virtual QString getOpenFileNameInternal(const QString & caption,
00138                                            const QString & filter=QString::null,
00139                                            const QString & directory=QString::null);
00140 
00141   virtual QString getSaveFileNameInternal(const QString & caption,
00142                                            const QString & filter=QString::null,
00143                                            const QString & selection=QString::null);
00144 
00145   virtual QStringList getOpenFileNamesInternal(const QString & caption,
00146                                                 const QString & filter=QString::null,
00147                                                 const QString & directory=QString::null);
00148 
00149   virtual QString getExistingDirectoryInternal(const QString & caption,
00150                                                 const QString & dir=QString::null);
00151 
00152 private:
00153   // Relates the process level to the answers selected by the user
00154   class Context {
00155   public:
00156     Context(bool q=false) {quiet=q;}
00157 
00158     QMap<uint, Answer> answers;
00159     bool quiet;
00160   };
00161   QStack<Context> _contexts;
00162   mutable QMutex _contextMutex;
00163 };
00164 
00165 inline bool Message::hasAutoAnswer(uint msgId) const
00166 {
00167   QMutexLocker ml(&_contextMutex);
00168   return _contexts.top().answers.contains(msgId);
00169 }
00170 
00171 inline bool Message::quiet() const
00172 {
00173   QMutexLocker ml(&_contextMutex);
00174   return _contexts.top().quiet;
00175 }
00176 
00177 inline Message::Answer Message::information(uint msgId, const QString & caption, const QString & text,
00178                                             bool againOption)
00179 {
00180   return information(msgId, caption, text, QString::null, QString::null, QString::null, againOption);
00181 }
00182 
00183 inline Message::Answer Message::information(uint msgId, const QString & caption, const QString & text,
00184                                                    QString answer0, bool againOption)
00185 {
00186   return information(msgId, caption, text, answer0, QString::null, QString::null, againOption);
00187 }
00188 
00189 inline Message::Answer Message::information(uint msgId, const QString & caption, const QString & text,
00190                                                    QString answer0, QString answer1, bool againOption)
00191 {
00192   return information(msgId, caption, text, answer0, answer1, QString::null, againOption);
00193 }
00194 
00195 inline Message::Answer Message::warning(uint msgId, const QString & caption, const QString & text,
00196                                                bool againOption)
00197 {
00198   return warning(msgId, caption, text, QString::null, QString::null, QString::null, againOption);
00199 }
00200 
00201 inline Message::Answer Message::warning(uint msgId, const QString & caption, const QString & text,
00202                                                QString answer0, bool againOption)
00203 {
00204   return warning(msgId, caption, text, answer0, QString::null, QString::null, againOption);
00205 }
00206 
00207 inline Message::Answer Message::warning(uint msgId, const QString & caption, const QString & text,
00208                                                QString answer0, QString answer1, bool againOption)
00209 {
00210   return warning(msgId, caption, text, answer0, answer1, QString::null, againOption);
00211 }
00212 
00213 inline Message::Answer Message::critical(uint msgId, const QString & caption, const QString & text,
00214                                                 bool againOption)
00215 {
00216   return critical(msgId, caption, text, QString::null, QString::null, QString::null, againOption);
00217 }
00218 
00219 inline Message::Answer Message::critical(uint msgId, const QString & caption, const QString & text,
00220                                                 QString answer0, bool againOption)
00221 {
00222   return critical(msgId, caption, text, answer0, QString::null, QString::null, againOption);
00223 }
00224 
00225 inline Message::Answer Message::critical(uint msgId, const QString & caption, const QString & text,
00226                                                 QString answer0, QString answer1, bool againOption)
00227 {
00228   return critical(msgId, caption, text, answer0, answer1, QString::null, againOption);
00229 }
00230 
00231 inline Message::Answer Message::question(uint msgId, const QString & caption, const QString & text,
00232                                                 bool againOption)
00233 {
00234   return question(msgId, caption, text, QString::null, QString::null, QString::null, againOption);
00235 }
00236 inline Message::Answer Message::question(uint msgId, const QString & caption, const QString & text,
00237                                                 QString answer0, bool againOption)
00238 {
00239   return question(msgId, caption, text, answer0, QString::null, QString::null, againOption);
00240 }
00241 inline Message::Answer Message::question(uint msgId, const QString & caption, const QString & text,
00242                                                 QString answer0, QString answer1, bool againOption)
00243 {
00244   return question(msgId, caption, text, answer0, answer1, QString::null, againOption);
00245 }
00246 
00247 } // namespace QGpCoreTools
00248 
00249 #endif // MESSAGE_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines