Back to index

fet  5.18.0
longtextmessagebox.cpp
Go to the documentation of this file.
00001 /*
00002 File longtextmessagebox.cpp
00003 */
00004 
00005 /***************************************************************************
00006                           longtextmessagebox.cpp  -  description
00007                              -------------------
00008     begin                : 27 June 2009
00009     copyright            : (C) 2009 by Lalescu Liviu
00010     email                : Please see http://lalescu.ro/liviu/ for details about contacting Liviu Lalescu (in particular, you can find here the e-mail address)
00011  ***************************************************************************/
00012 
00013 /***************************************************************************
00014  *                                                                         *
00015  *   This program is free software; you can redistribute it and/or modify  *
00016  *   it under the terms of the GNU General Public License as published by  *
00017  *   the Free Software Foundation; either version 2 of the License, or     *
00018  *   (at your option) any later version.                                   *
00019  *                                                                         *
00020  ***************************************************************************/
00021 
00022 #include "longtextmessagebox.h"
00023 
00024 #include <QMessageBox>
00025 
00026 #include <QVBoxLayout>
00027 #include <QHBoxLayout>
00028 #include <QPlainTextEdit>
00029 #include <QPushButton>
00030 
00031 #include "centerwidgetonscreen.h"
00032 
00033 const int MIN_WIDTH=420;  //golden ratio 1.618 for min dimensions :-)
00034 const int MAX_WIDTH=1000;
00035 const int MIN_HEIGHT=260;
00036 const int MAX_HEIGHT=650;
00037 
00038 const int LARGE_MIN_WIDTH=590;
00039 const int LARGE_MAX_WIDTH=1000;
00040 const int LARGE_MIN_HEIGHT=380;
00041 const int LARGE_MAX_HEIGHT=650;
00042 
00043 const int MEDIUM_MIN_WIDTH=461;
00044 const int MEDIUM_MAX_WIDTH=1000;
00045 const int MEDIUM_MIN_HEIGHT=285;
00046 const int MEDIUM_MAX_HEIGHT=650;
00047 
00048 int LongTextMessageBox::confirmationWithDimensions
00049  ( QWidget * parent, const QString & title, const QString & text,
00050  const QString& button0Text, const QString& button1Text, const QString& button2Text,
00051  int defaultButton, int escapeButton, int MINW, int MAXW, int MINH, int MAXH )
00052 {
00053        if(button0Text==QString() || button1Text==QString() || button2Text!=QString()){
00054               QMessageBox::critical(parent, tr("FET critical"), tr("You have met a FET bug. The problem is in file"
00055                " %1 line %2, the reason is that a confirmation dialog box does not get exactly 2 arguments. Please report bug. FET will now continue."
00056                " You probably don't have any problems with your data file - you can save it.").arg(__FILE__).arg(__LINE__));
00057        }
00058 
00059        QDialog dialog(parent);
00060        dialog.setWindowTitle(title);
00061        
00062        QVBoxLayout* vl=new QVBoxLayout(&dialog);
00063        QPlainTextEdit* te=new QPlainTextEdit();
00064        te->setPlainText(text);
00065        te->setReadOnly(true);
00066        
00067        QPushButton* pb0=NULL;
00068        if(button0Text!=QString()){
00069               pb0=new QPushButton(button0Text);
00070               if(defaultButton==0)
00071                      connect(pb0, SIGNAL(clicked()), &dialog, SLOT(accept()));
00072               if(escapeButton==0)
00073                      connect(pb0, SIGNAL(clicked()), &dialog, SLOT(reject()));
00074        }
00075        
00076        QPushButton* pb1=NULL;
00077        if(button1Text!=QString()){
00078               pb1=new QPushButton(button1Text);
00079               if(defaultButton==1)
00080                      connect(pb1, SIGNAL(clicked()), &dialog, SLOT(accept()));
00081               if(escapeButton==1)
00082                      connect(pb1, SIGNAL(clicked()), &dialog, SLOT(reject()));
00083        }
00084        
00085        QPushButton* pb2=NULL;
00086        if(button2Text!=QString()){
00087               pb2=new QPushButton(button2Text);
00088               if(defaultButton==2)
00089                      connect(pb2, SIGNAL(clicked()), &dialog, SLOT(accept()));
00090               if(escapeButton==2)
00091                      connect(pb2, SIGNAL(clicked()), &dialog, SLOT(reject()));
00092        }
00093        
00094        QHBoxLayout* hl=new QHBoxLayout();
00095        hl->addStretch(1);
00096        if(pb0!=NULL)
00097               hl->addWidget(pb0);
00098        if(pb1!=NULL)
00099               hl->addWidget(pb1);
00100        if(pb2!=NULL)
00101               hl->addWidget(pb2);
00102               
00103        vl->addWidget(te);
00104        vl->addLayout(hl);
00105        
00106        if(pb0!=NULL && defaultButton==0){
00107               pb0->setDefault(true);
00108               pb0->setFocus();
00109        }
00110        if(pb1!=NULL && defaultButton==1){
00111               pb1->setDefault(true);
00112               pb1->setFocus();
00113        }
00114        if(pb2!=NULL && defaultButton==2){
00115               pb2->setDefault(true);
00116               pb2->setFocus();
00117        }
00118        
00119        int w=dialog.sizeHint().width();
00120        int h=dialog.sizeHint().height();
00121        if(w>MAXW) w=MAXW;
00122        if(w<MINW) w=MINW;
00123        if(h>MAXH) h=MAXH;
00124        if(h<MINH) h=MINH;
00125        dialog.resize(w, h);
00126        if(parent==0)
00127               forceCenterWidgetOnScreen(&dialog);
00128        
00129        int b=dialog.exec();
00130        
00131        if(b==QDialog::Accepted){
00132               //cout<<"accepted"<<endl;
00133               return defaultButton;
00134        }
00135        else{
00136               //cout<<"rejected"<<endl;
00137               return escapeButton;
00138        }
00139 }
00140 
00141 int LongTextMessageBox::confirmation
00142  ( QWidget * parent, const QString & title, const QString & text,
00143  const QString& button0Text, const QString& button1Text, const QString& button2Text,
00144  int defaultButton, int escapeButton)
00145 {
00146        return LongTextMessageBox::confirmationWithDimensions(parent, title, text,
00147               button0Text, button1Text, button2Text,
00148               defaultButton, escapeButton,
00149               MIN_WIDTH, MAX_WIDTH, MIN_HEIGHT, MAX_HEIGHT);
00150 }
00151 
00152 int LongTextMessageBox::largeConfirmation
00153  ( QWidget * parent, const QString & title, const QString & text,
00154  const QString& button0Text, const QString& button1Text, const QString& button2Text,
00155  int defaultButton, int escapeButton)
00156 {
00157        return LongTextMessageBox::confirmationWithDimensions(parent, title, text,
00158               button0Text, button1Text, button2Text,
00159               defaultButton, escapeButton,
00160               LARGE_MIN_WIDTH, LARGE_MAX_WIDTH, LARGE_MIN_HEIGHT, LARGE_MAX_HEIGHT);
00161 }
00162 
00163 int LongTextMessageBox::mediumConfirmation
00164  ( QWidget * parent, const QString & title, const QString & text,
00165  const QString& button0Text, const QString& button1Text, const QString& button2Text,
00166  int defaultButton, int escapeButton)
00167 {
00168        return LongTextMessageBox::confirmationWithDimensions(parent, title, text,
00169               button0Text, button1Text, button2Text,
00170               defaultButton, escapeButton,
00171               MEDIUM_MIN_WIDTH, MEDIUM_MAX_WIDTH, MEDIUM_MIN_HEIGHT, MEDIUM_MAX_HEIGHT);
00172 }
00173 
00174 void LongTextMessageBox::informationWithDimensions
00175  ( QWidget * parent, const QString & title, const QString & text, int MINW, int MAXW, int MINH, int MAXH)
00176 {
00177        QDialog dialog(parent);
00178        dialog.setWindowTitle(title);
00179        
00180        QVBoxLayout* vl=new QVBoxLayout(&dialog);
00181        QPlainTextEdit* te=new QPlainTextEdit();
00182        te->setPlainText(text);
00183        te->setReadOnly(true);
00184        
00185        QPushButton* pb=new QPushButton(tr("OK"));
00186        connect(pb, SIGNAL(clicked()), &dialog, SLOT(accept()));
00187        
00188        QHBoxLayout* hl=new QHBoxLayout();
00189        hl->addStretch(1);
00190        hl->addWidget(pb);
00191               
00192        vl->addWidget(te);
00193        vl->addLayout(hl);
00194        
00195        pb->setDefault(true);
00196        pb->setFocus();
00197        
00198        int w=dialog.sizeHint().width();
00199        int h=dialog.sizeHint().height();
00200        if(w>MAXW) w=MAXW;
00201        if(w<MINW) w=MINW;
00202        if(h>MAXH) h=MAXH;
00203        if(h<MINH) h=MINH;
00204        dialog.resize(w, h);
00205        if(parent==0)
00206               forceCenterWidgetOnScreen(&dialog);
00207        
00208        dialog.exec();
00209 }
00210 
00211 void LongTextMessageBox::information
00212  ( QWidget * parent, const QString & title, const QString & text)
00213 {
00214        LongTextMessageBox::informationWithDimensions(parent, title, text, MIN_WIDTH, MAX_WIDTH, MIN_HEIGHT, MAX_HEIGHT);
00215 }
00216 
00217 void LongTextMessageBox::largeInformation
00218  ( QWidget * parent, const QString & title, const QString & text)
00219 {
00220        LongTextMessageBox::informationWithDimensions(parent, title, text, LARGE_MIN_WIDTH, LARGE_MAX_WIDTH, LARGE_MIN_HEIGHT, LARGE_MAX_HEIGHT);
00221 }
00222 
00223 void LongTextMessageBox::mediumInformation
00224  ( QWidget * parent, const QString & title, const QString & text)
00225 {
00226        LongTextMessageBox::informationWithDimensions(parent, title, text, MEDIUM_MIN_WIDTH, MEDIUM_MAX_WIDTH, MEDIUM_MIN_HEIGHT, MEDIUM_MAX_HEIGHT);
00227 }