QxtGuidGenDialog.h

00001 
00002 #ifndef QXTGUIDGENDIALOG_H_9B3B2A65_DC73_4e55_97BB_E81AD7C8BE32
00003 #define QXTGUIDGENDIALOG_H_9B3B2A65_DC73_4e55_97BB_E81AD7C8BE32
00004 
00005 #include <QApplication>
00006 #include <QClipboard>
00007 #include <QSettings>
00008 #include <QUuid>
00009 
00010 #include "ui_QxtGuidGenDialog.h"
00011 
00012 /*!
00013 ** \mainpage GUID Generator Dialog
00014 **
00015 ** This class can be included into any Qt project to cause a dialog window to
00016 **  be opened for the user to generate a GUID value.
00017 **
00018 ** \image html screenshot.png
00019 **
00020 ** This program is designed to emulate, exactly, the windows version of the 
00021 **  guidgen.exe program.  It produces the same basic results with a few added
00022 **  features, such as auto-clipboard copy, and uppercase/lowercase output.
00023 **
00024 ** My primary reason for producing this program was to produce a format output
00025 **  compatible with stamping the tops of header files with dual-include
00026 **  protection.  My problem with implementing header include protection methods
00027 **  is automated build systems have been stealing my formats.  Ok, maybe not
00028 **  mine, but in the past I was using a format similar to the following:
00029 **
00030 ** \code
00031 ** #ifndef MYHEADER_H
00032 ** #define MYHEADER_H
00033 ** ...do some stuff
00034 ** #endif
00035 ** \endcode
00036 **
00037 ** But, one automated build system I was using suddenly, on an upgrade, happened
00038 **  to change to use that same format.  I had similar file names, and ended up having a
00039 **  collision with their include protection naming scheme and mine.  I have seen
00040 **  in the past, some naming conventions including a GUID value imbedded in the
00041 **  string value.  Reluctantly I have resolved to adopt the same technique.  My
00042 **  header include protection strings now look like the following:
00043 **
00044 ** \code
00045 ** #ifndef MYHEADER_H_E963A104_7E3D_4CDA_A47EB01B433D67FD
00046 ** #define MYHEADER_H_E963A104_7E3D_4CDA_A47EB01B433D67FD
00047 ** ...do some stuff
00048 ** #endif
00049 ** \endcode
00050 **
00051 ** It may look like an overkill, but use it and you won't have to worry about
00052 **  somebody stomping on your include header protection.
00053 **
00054 ** The problem I had with using this format was the GUID string offered by the
00055 **  Microsoft version of guidgen.exe had to be reformatted by hand to get it to
00056 **  work.  So, one lazy Saturday afternoon later and I've produced this.
00057 **
00058 ** Enjoy!
00059 **
00060 **
00061 */
00062 class QxtGuidGenDialog : public QDialog, private Ui::QxtGuidGenDialog
00063 {
00064   Q_OBJECT
00065 
00066   public:
00067 
00068     QxtGuidGenDialog()
00069     {
00070       setupUi(this);
00071 
00072       load();
00073 
00074       newid();
00075 
00076       update();
00077 
00078       connect( rbOleCreate,  SIGNAL(released()), this, SLOT(update()) );
00079       connect( rbDefGuid,    SIGNAL(released()), this, SLOT(update()) );
00080       connect( rbStaticGuid, SIGNAL(released()), this, SLOT(update()) );
00081       connect( rbRegistry,   SIGNAL(released()), this, SLOT(update()) );
00082       connect( rbHeader,     SIGNAL(released()), this, SLOT(update()) );
00083       connect( cbAutocopy,   SIGNAL(released()), this, SLOT(update()) );
00084       connect( cbUppercase,  SIGNAL(released()), this, SLOT(update()) );
00085 
00086     }
00087 
00088     ~QxtGuidGenDialog()
00089     {
00090       if( cbAutocopy-> isChecked() )
00091         on_pbCopy_released();
00092 
00093       save();
00094     }
00095 
00096     QString iniFilename()
00097     {
00098       return( qApp-> applicationDirPath() + "/QxtGuidGenDialog.ini" );
00099     }
00100 
00101     void load()
00102     {
00103       QSettings settings( iniFilename(), QSettings::IniFormat, this );
00104 
00105       restoreGeometry( settings.value("geometry").toByteArray() );
00106 
00107       cbAutocopy->  setChecked( settings.value("autocopy").toBool()  );
00108       cbUppercase-> setChecked( settings.value("uppercase").toBool() );
00109 
00110       QString style = settings.value("style").toString();
00111       if( style == "oleCreate"  ) rbOleCreate->  setChecked(true);
00112       if( style == "defGuid"    ) rbDefGuid->    setChecked(true);
00113       if( style == "staticGuid" ) rbStaticGuid-> setChecked(true);
00114       if( style == "registry"   ) rbRegistry->   setChecked(true);
00115       if( style == "header"     ) rbHeader->     setChecked(true);
00116 
00117     }
00118 
00119     void save()
00120     {
00121       QSettings settings( iniFilename(), QSettings::IniFormat, this );
00122 
00123       settings.setValue( "geometry", saveGeometry() );
00124 
00125       settings.setValue( "autocopy",  cbAutocopy->  isChecked() );
00126       settings.setValue( "uppercase", cbUppercase-> isChecked() );
00127 
00128       QString style = "none";
00129       if( rbOleCreate->  isChecked() ) style = "oleCreate";
00130       if( rbDefGuid->    isChecked() ) style = "defGuid";
00131       if( rbStaticGuid-> isChecked() ) style = "staticGuid";
00132       if( rbRegistry->   isChecked() ) style = "registry";
00133       if( rbHeader->     isChecked() ) style = "header";
00134 
00135       settings.setValue( "style", style );
00136     }
00137 
00138     void set()
00139     {
00140       text-> clear();
00141 
00142       if( rbOleCreate-> isChecked() )
00143       {
00144         text-> append( "// " + id.toString() );
00145         text-> append( format("IMPLEMENT_OLECREATE(<<class>>, <<external_name>>, 0x%1, 0x%2, 0x%3, 0x%4, 0x%5, 0x%6, 0x%7, 0x%8, 0x%9, 0x%10, 0x%11);") );
00146       }
00147 
00148       if( rbDefGuid-> isChecked() )
00149       {
00150         text-> append( "//" + id.toString() );
00151         text-> append( format("DEFINE_GUID(<<name>>, 0x%1, 0x%2, 0x%3, 0x%4, 0x%5, 0x%6, 0x%7, 0x%8, 0x%9, 0x%10, 0x%11);") );
00152       }
00153 
00154       if( rbStaticGuid-> isChecked() )
00155       {
00156         text-> append( "//" + id.toString() );
00157         text-> append( format("static const GUID <<name>> = { 0x%1, 0x%2, 0x%3, { 0x%4, 0x%5, 0x%6, 0x%7, 0x%8, 0x%9, 0x%10, 0x%11 } };") );
00158       }
00159 
00160       if( rbRegistry-> isChecked() )
00161       {
00162         QString value = format("{%1-%2-%3-%4%5-%6%7%8%9%10%11}");
00163 
00164         if( cbUppercase-> isChecked() )
00165           value = value.toUpper();
00166 
00167         text-> append( value );
00168       }
00169 
00170       if( rbHeader-> isChecked() )
00171       {
00172         QString value = format("_%1_%2_%3_%4%5_%6%7%8%9%10%11");
00173 
00174         if( cbUppercase-> isChecked() )
00175           value = value.toUpper();
00176 
00177         text-> append( value );
00178       }
00179 
00180     }
00181 
00182     QString format( const QString formatString )
00183     {
00184       QString retVal = formatString
00185           .arg( ((GUID)id).Data1,    8, 16, QChar('0')  )
00186           .arg( ((GUID)id).Data2,    4, 16, QChar('0')  )
00187           .arg( ((GUID)id).Data3,    4, 16, QChar('0')  )
00188           .arg( ((GUID)id).Data4[0], 2, 16, QChar('0')  )
00189           .arg( ((GUID)id).Data4[1], 2, 16, QChar('0')  )
00190           .arg( ((GUID)id).Data4[2], 2, 16, QChar('0')  )
00191           .arg( ((GUID)id).Data4[3], 2, 16, QChar('0')  )
00192           .arg( ((GUID)id).Data4[4], 2, 16, QChar('0')  )
00193           .arg( ((GUID)id).Data4[5], 2, 16, QChar('0')  )
00194           .arg( ((GUID)id).Data4[6], 2, 16, QChar('0')  )
00195           .arg( ((GUID)id).Data4[7], 2, 16, QChar('0')  )
00196       ;
00197 
00198       return( retVal );
00199 
00200     }
00201 
00202     void newid()
00203     {
00204       id = QUuid::createUuid();
00205     }
00206 
00207   QUuid id;
00208 
00209   public slots: 
00210 
00211     void update()
00212     {
00213       set();
00214 
00215       if( cbAutocopy-> isChecked() )
00216         on_pbCopy_released();
00217 
00218     }
00219 
00220     void on_pbCopy_released()
00221     {
00222       QApplication::clipboard()-> setText( text-> toPlainText() );
00223     }
00224 
00225     void on_pbExit_released()
00226     {
00227       close();
00228     }
00229 
00230     void on_pbNew_released()
00231     {
00232       newid();
00233       update();
00234     }
00235 
00236     void on_pbCopyExit_released()
00237     {
00238       on_pbCopy_released();
00239       close();
00240     }
00241 
00242 };
00243 
00244 #endif // QGUIDGEN_H_9B3B2A65_DC73_4e55_97BB_E81AD7C8BE32
00245 




~ ~ ~ ~ ~ ~
Source Code without Comments is like a Cranberry Garland
without the berries. Don't create Texas sized problems...
Comment your Code!
~ ~ ~ ~ ~ ~
Author: Mark Petryk
Lorimark Solutions, LLC