1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: QCMakeWidgets.cxx,v $
6 Date: $Date: 2008-12-16 20:15:33 $
7 Version: $Revision: 1.2 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
18 #include "QCMakeWidgets.h"
22 #include <QFileDialog>
23 #include <QToolButton>
24 #include <QResizeEvent>
26 QCMakeFileEditor::QCMakeFileEditor(QWidget
* p
, const QString
& var
)
27 : QLineEdit(p
), Variable(var
)
29 this->ToolButton
= new QToolButton(this);
30 this->ToolButton
->setText("...");
31 this->ToolButton
->setCursor(QCursor(Qt::ArrowCursor
));
32 QObject::connect(this->ToolButton
, SIGNAL(clicked(bool)),
33 this, SLOT(chooseFile()));
36 QCMakeFilePathEditor::QCMakeFilePathEditor(QWidget
* p
, const QString
& var
)
37 : QCMakeFileEditor(p
, var
)
39 this->setCompleter(new QCMakeFileCompleter(this, false));
42 QCMakePathEditor::QCMakePathEditor(QWidget
* p
, const QString
& var
)
43 : QCMakeFileEditor(p
, var
)
45 this->setCompleter(new QCMakeFileCompleter(this, true));
48 void QCMakeFileEditor::resizeEvent(QResizeEvent
* e
)
50 // make the tool button fit on the right side
51 int h
= e
->size().height();
52 // move the line edit to make room for the tool button
53 this->setContentsMargins(0, 0, h
, 0);
54 // put the tool button in its place
55 this->ToolButton
->resize(h
, h
);
56 this->ToolButton
->move(this->width() - h
, 0);
59 void QCMakeFilePathEditor::chooseFile()
61 // choose a file and set it
63 QFileInfo
info(this->text());
65 if(this->Variable
.isEmpty())
67 title
= tr("Select File");
71 title
= tr("Select File for %1");
72 title
= title
.arg(this->Variable
);
74 this->fileDialogExists(true);
75 path
= QFileDialog::getOpenFileName(this, title
, info
.absolutePath());
76 this->fileDialogExists(false);
80 this->setText(QDir::fromNativeSeparators(path
));
84 void QCMakePathEditor::chooseFile()
86 // choose a file and set it
89 if(this->Variable
.isEmpty())
91 title
= tr("Select Path");
95 title
= tr("Select Path for %1");
96 title
= title
.arg(this->Variable
);
98 this->fileDialogExists(true);
99 path
= QFileDialog::getExistingDirectory(this, title
, this->text());
100 this->fileDialogExists(false);
103 this->setText(QDir::fromNativeSeparators(path
));
107 // use same QDirModel for all completers
108 static QDirModel
* fileDirModel()
110 static QDirModel
* m
= NULL
;
117 static QDirModel
* pathDirModel()
119 static QDirModel
* m
= NULL
;
123 m
->setFilter(QDir::AllDirs
| QDir::Drives
| QDir::NoDotAndDotDot
);
128 QCMakeFileCompleter::QCMakeFileCompleter(QObject
* o
, bool dirs
)
131 QDirModel
* m
= dirs
? pathDirModel() : fileDirModel();
135 QString
QCMakeFileCompleter::pathFromIndex(const QModelIndex
& idx
) const
137 return QDir::fromNativeSeparators(QCompleter::pathFromIndex(idx
));