ctkDirectoryButton.cpp

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Library:   CTK
00004  
00005   Copyright (c) 2010  Kitware Inc.
00006 
00007   Licensed under the Apache License, Version 2.0 (the "License");
00008   you may not use this file except in compliance with the License.
00009   You may obtain a copy of the License at
00010 
00011       http://www.commontk.org/LICENSE
00012 
00013   Unless required by applicable law or agreed to in writing, software
00014   distributed under the License is distributed on an "AS IS" BASIS,
00015   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016   See the License for the specific language governing permissions and
00017   limitations under the License.
00018  
00019 =========================================================================*/
00020 
00021 // Qt includes
00022 #include <QHBoxLayout>
00023 #include <QPushButton>
00024 #include <QStyle>
00025 
00026 // CTK includes
00027 #include "ctkDirectoryButton.h"
00028 
00029 //-----------------------------------------------------------------------------
00030 class ctkDirectoryButtonPrivate: public ctkPrivate<ctkDirectoryButton>
00031 {
00032 public:
00033   ctkDirectoryButtonPrivate();
00034   void init();
00035 
00036   QDir         Directory;
00037   QPushButton* PushButton;
00038   QString      DialogCaption;
00039   ctkDirectoryButton::Options DialogOptions;
00040 };
00041 
00042 //-----------------------------------------------------------------------------
00043 ctkDirectoryButtonPrivate::ctkDirectoryButtonPrivate()
00044 {
00045   this->DialogOptions = ctkDirectoryButton::ShowDirsOnly;
00046 }
00047 
00048 //-----------------------------------------------------------------------------
00049 void ctkDirectoryButtonPrivate::init()
00050 {
00051   CTK_P(ctkDirectoryButton);
00052   this->PushButton = new QPushButton(p);
00053   QObject::connect(this->PushButton, SIGNAL(clicked()), p, SLOT(browse()));
00054   QHBoxLayout* l = new QHBoxLayout(p);
00055   l->addWidget(this->PushButton);
00056   l->setContentsMargins(0,0,0,0);
00057   p->setLayout(l);
00058 }
00059 
00060 //-----------------------------------------------------------------------------
00061 ctkDirectoryButton::ctkDirectoryButton(QWidget * parentWidget)
00062   :QWidget(parentWidget)
00063 {
00064   CTK_INIT_PRIVATE(ctkDirectoryButton);
00065   CTK_D(ctkDirectoryButton);
00066   d->init();
00067   d->PushButton->setIcon(this->style()->standardIcon(QStyle::SP_DirIcon));
00068 }
00069     
00070 //-----------------------------------------------------------------------------
00071 ctkDirectoryButton::ctkDirectoryButton(const QString& dir, 
00072                                        QWidget * parentWidget)
00073   :QWidget(parentWidget)
00074 {
00075   CTK_INIT_PRIVATE(ctkDirectoryButton);
00076   CTK_D(ctkDirectoryButton);
00077   d->init();
00078   d->Directory = QDir(dir);
00079   d->PushButton->setText(d->Directory.path());
00080   d->PushButton->setIcon(this->style()->standardIcon(QStyle::SP_DirIcon));
00081 }
00082 
00083 //-----------------------------------------------------------------------------
00084 ctkDirectoryButton::ctkDirectoryButton(
00085   const QIcon & icon, const QString& dir, QWidget * parentWidget)
00086   :QWidget(parentWidget)
00087 {
00088   CTK_INIT_PRIVATE(ctkDirectoryButton);
00089   CTK_D(ctkDirectoryButton);
00090   d->init();
00091   d->Directory = QDir(dir);
00092   d->PushButton->setText(d->Directory.path());
00093   d->PushButton->setIcon(icon);
00094 }
00095 
00096 //-----------------------------------------------------------------------------
00097 void ctkDirectoryButton::setDirectory(const QString& dir)
00098 {
00099   CTK_D(ctkDirectoryButton);
00100   QDir newDirectory(dir);
00101   if (d->Directory == newDirectory )
00102     {
00103     return;
00104     }
00105   d->Directory = newDirectory;
00106   d->PushButton->setText(d->Directory.path());
00107   emit directoryChanged(d->Directory.path());
00108 }
00109 
00110 //-----------------------------------------------------------------------------
00111 QString ctkDirectoryButton::directory()const
00112 {
00113   CTK_D(const ctkDirectoryButton);
00114   return d->Directory.path();
00115 }
00116 
00117 //-----------------------------------------------------------------------------
00118 void ctkDirectoryButton::setCaption(const QString& caption)
00119 {
00120   CTK_D(ctkDirectoryButton);
00121   d->DialogCaption = caption;
00122 }
00123 
00124 //-----------------------------------------------------------------------------
00125 const QString& ctkDirectoryButton::caption()const
00126 {
00127   CTK_D(const ctkDirectoryButton);
00128   return d->DialogCaption;
00129 }
00130 
00131 //-----------------------------------------------------------------------------
00132 void ctkDirectoryButton::setOptions(const Options& dialogOptions)
00133 {
00134   CTK_D(ctkDirectoryButton);
00135   d->DialogOptions = dialogOptions;
00136 }
00137 
00138 //-----------------------------------------------------------------------------
00139 const ctkDirectoryButton::Options& ctkDirectoryButton::options()const
00140 {
00141   CTK_D(const ctkDirectoryButton);
00142   return d->DialogOptions;
00143 }
00144 
00145 //-----------------------------------------------------------------------------
00146 void ctkDirectoryButton::browse()
00147 {
00148   CTK_D(ctkDirectoryButton);
00149   QString dir = 
00150     QFileDialog::getExistingDirectory(
00151       this, 
00152       d->DialogCaption.isEmpty() ? this->toolTip() : d->DialogCaption, 
00153       d->Directory.path(), 
00154       QFlags<QFileDialog::Option>(int(d->DialogOptions)));
00155   // An empty directory means that the user cancelled the dialog.
00156   if (dir.isEmpty())
00157     {
00158     return;
00159     }
00160   this->setDirectory(dir);
00161 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated on 21 May 2010 for CTK by  doxygen 1.6.1