1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
/*
* The information in this file is
* Copyright(c) 2007 Ball Aerospace & Technologies Corporation
* and is subject to the terms and conditions of the
* GNU Lesser General Public License Version 2.1
* The license text is available from
* http://www.gnu.org/licenses/lgpl.html
*/
#include <QtGui/QApplication>
#include <QtGui/QBitmap>
#include <QtGui/QCompleter>
#include <QtGui/QDirModel>
#include <QtGui/QFileDialog>
#include <QtGui/QFocusEvent>
#include <QtGui/QHBoxLayout>
#include "FileBrowser.h"
// Browse button
static const char* const FileOpenIcon[] =
{
"16 16 5 1",
"# c #000000",
"c c #808000",
". c #c0c0c0",
"b c #ffff00",
"a c #ffffff",
"................",
"..........###...",
".........#...#.#",
"..............##",
"..###........###",
".#aba#######....",
".#babababab#....",
".#ababababa#....",
".#baba##########",
".#aba#ccccccccc#",
".#ba#ccccccccc#.",
".#a#ccccccccc#..",
".##ccccccccc#...",
".###########....",
"................",
"................"
};
FileBrowser::FileBrowser(QWidget* pParent) :
QWidget(pParent),
mExistingFile(true),
mBrowseType(FileBrowser::File)
{
// Filename edit
QCompleter* pCompleter = new QCompleter(this);
QDirModel* pDirModel = new QDirModel(QStringList(), QDir::NoDotAndDotDot | QDir::AllDirs | QDir::AllEntries,
QDir::Name | QDir::DirsFirst, pCompleter);
pCompleter->setModel(pDirModel);
mpFileEdit = new QLineEdit(this);
mpFileEdit->setCompleter(pCompleter);
mpFileEdit->installEventFilter(this);
QPixmap pixOpen(FileOpenIcon);
pixOpen.setMask(pixOpen.createHeuristicMask());
QIcon icnBrowse(pixOpen);
mpBrowseButton = new QPushButton(icnBrowse, "", this);
mpBrowseButton->setFixedWidth(25);
mpBrowseButton->installEventFilter(this);
// Layout
QHBoxLayout* pLayout = new QHBoxLayout(this);
pLayout->setMargin(0);
pLayout->setSpacing(0);
pLayout->addWidget(mpFileEdit, 100);
pLayout->addWidget(mpBrowseButton);
// Initialization
setFocusPolicy(Qt::StrongFocus);
setFocusProxy(mpFileEdit);
setAutoFillBackground(true);
// Connections
connect(mpFileEdit, SIGNAL(textChanged(const QString&)), this, SIGNAL(filenameChanged(const QString&)));
connect(mpBrowseButton, SIGNAL(clicked()), this, SLOT(browse()));
}
FileBrowser::~FileBrowser()
{
}
void FileBrowser::setFilename(const QString& filename)
{
if (filename != getFilename())
{
mpFileEdit->setText(filename);
}
}
QString FileBrowser::getFilename() const
{
return mpFileEdit->text();
}
void FileBrowser::setBrowseCaption(const QString& caption)
{
mBrowseCaption = caption;
}
QString FileBrowser::getBrowseCaption() const
{
return mBrowseCaption;
}
void FileBrowser::setBrowseDirectory(const QString& directory)
{
mBrowseDirectory = directory;
}
QString FileBrowser::getBrowseDirectory() const
{
return mBrowseDirectory;
}
void FileBrowser::setBrowseFileFilters(const QString& filters)
{
if (filters != mBrowseFilters)
{
mBrowseFilters = filters;
// Update the filters in the completer
QStringList dirFilters;
QStringList filterList = mBrowseFilters.split(";;", QString::SkipEmptyParts);
if (filterList.empty() == false)
{
// Remove the All Files filter
for (int i = 0; i < filterList.count(); ++i)
{
QString filter = filterList[i];
if (filter.startsWith("All Files") == true)
{
filterList.removeAt(i);
break;
}
}
// Convert the filters to the directory name filter format
if (filterList.empty() == false)
{
for (int i = 0; i < filterList.count(); ++i)
{
QString filter = filterList[i];
int startPos = filter.indexOf("(") + 1;
int numChars = filter.lastIndexOf(")") - startPos;
filter = filter.mid(startPos, numChars);
dirFilters += filter.split(' ', QString::SkipEmptyParts);
}
}
}
QCompleter* pCompleter = mpFileEdit->completer();
if (pCompleter != NULL)
{
QDirModel* pDirModel = dynamic_cast<QDirModel*>(pCompleter->model());
if (pDirModel != NULL)
{
pDirModel->setNameFilters(dirFilters);
}
}
}
}
QString FileBrowser::getBrowseFileFilters() const
{
return mBrowseFilters;
}
void FileBrowser::setBrowseExistingFile(bool bExistingFile)
{
mExistingFile = bExistingFile;
}
bool FileBrowser::isBrowseExistingFile() const
{
return mExistingFile;
}
void FileBrowser::setBrowseType(BrowseType browseType)
{
mBrowseType = browseType;
}
FileBrowser::BrowseType FileBrowser::getBrowseType() const
{
return mBrowseType;
}
bool FileBrowser::eventFilter(QObject* pObject, QEvent* pEvent)
{
if (pEvent != NULL)
{
if (pEvent->type() == QEvent::FocusOut)
{
QWidget* pFocusWidget = QApplication::focusWidget();
if ((pFocusWidget != mpFileEdit) && (pFocusWidget != mpBrowseButton))
{
QFocusEvent* pFocusEvent = static_cast<QFocusEvent*>(pEvent);
QApplication::sendEvent(this, pFocusEvent);
}
}
}
return QWidget::eventFilter(pObject, pEvent);
}
void FileBrowser::browse()
{
// Remove the event filter on the browse button to prevent the focus
// out event from being sent when the browse dialog is invoked
mpBrowseButton->removeEventFilter(this);
// Get the initial browse directory
QString browseDirectory = getFilename();
if (browseDirectory.isEmpty() == true)
{
browseDirectory = mBrowseDirectory;
}
// Get the filename from the user
QString filename;
if (mBrowseType == FileBrowser::File)
{
if (mExistingFile == true)
{
filename = QFileDialog::getOpenFileName(this, mBrowseCaption, browseDirectory, mBrowseFilters);
}
else
{
filename = QFileDialog::getSaveFileName(this, mBrowseCaption, browseDirectory, mBrowseFilters);
}
}
else
{
filename = QFileDialog::getExistingDirectory(this, mBrowseCaption, browseDirectory);
}
if (filename.isEmpty() == false)
{
// Set the edit box text
setFilename(filename);
setFocus();
}
// Reinstall the event filter
mpBrowseButton->installEventFilter(this);
}
|