2014-12-31 21:00:34 +00:00
|
|
|
/*
|
2015-06-10 21:27:11 +00:00
|
|
|
* This file Copyright (C) 2014-2015 Mnemosyne LLC
|
2014-12-31 21:00:34 +00:00
|
|
|
*
|
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-12-31 23:54:52 +00:00
|
|
|
#include <QApplication>
|
2015-04-15 00:08:24 +00:00
|
|
|
#include <QDir>
|
2014-12-31 21:00:34 +00:00
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QFileIconProvider>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QStyle>
|
2014-12-31 23:54:52 +00:00
|
|
|
#include <QStyleOptionToolButton>
|
|
|
|
#include <QStylePainter>
|
2014-12-31 21:00:34 +00:00
|
|
|
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "PathButton.h"
|
|
|
|
#include "Utils.h"
|
2014-12-31 21:00:34 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
PathButton::PathButton(QWidget* parent) :
|
|
|
|
QToolButton(parent),
|
|
|
|
myMode(DirectoryMode),
|
|
|
|
myTitle(),
|
|
|
|
myNameFilter(),
|
|
|
|
myPath()
|
2014-12-31 21:00:34 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed));
|
|
|
|
setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
|
|
|
|
setText(tr("(None)")); // for minimum width
|
2014-12-31 22:27:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
updateAppearance();
|
2014-12-31 21:00:34 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
connect(this, SIGNAL(clicked()), this, SLOT(onClicked()));
|
2014-12-31 21:00:34 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void PathButton::setMode(Mode mode)
|
2014-12-31 21:00:34 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (myMode == mode)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-12-31 21:00:34 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
myMode = mode;
|
2014-12-31 21:00:34 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
updateAppearance();
|
2014-12-31 21:00:34 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void PathButton::setTitle(QString const& title)
|
2014-12-31 21:00:34 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
myTitle = title;
|
2014-12-31 21:00:34 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void PathButton::setNameFilter(QString const& nameFilter)
|
2014-12-31 21:00:34 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
myNameFilter = nameFilter;
|
2014-12-31 21:00:34 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void PathButton::setPath(QString const& path)
|
2014-12-31 21:00:34 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (myPath == path)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-12-31 21:00:34 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
myPath = QDir::toNativeSeparators(Utils::removeTrailingDirSeparator(path));
|
2014-12-31 21:00:34 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
updateAppearance();
|
2014-12-31 21:00:34 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
emit pathChanged(myPath);
|
2014-12-31 21:00:34 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QString const& PathButton::path() const
|
2014-12-31 21:00:34 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return myPath;
|
2014-12-31 21:00:34 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QSize PathButton::sizeHint() const
|
2015-01-25 19:55:05 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QSize const sh(QToolButton::sizeHint());
|
2017-04-19 12:04:45 +00:00
|
|
|
return QSize(qMin(sh.width(), 150), sh.height());
|
2015-01-25 19:55:05 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void PathButton::paintEvent(QPaintEvent* /*event*/)
|
2014-12-31 23:54:52 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QStylePainter painter(this);
|
|
|
|
QStyleOptionToolButton option;
|
|
|
|
initStyleOption(&option);
|
2014-12-31 23:54:52 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QSize const fakeContentSize(qMax(100, qApp->globalStrut().width()), qMax(100, qApp->globalStrut().height()));
|
|
|
|
QSize const fakeSizeHint = style()->sizeFromContents(QStyle::CT_ToolButton, &option, fakeContentSize, this);
|
2014-12-31 23:54:52 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int textWidth = width() - (fakeSizeHint.width() - fakeContentSize.width()) - iconSize().width() - 6;
|
|
|
|
|
|
|
|
if (popupMode() == MenuButtonPopup)
|
|
|
|
{
|
|
|
|
textWidth -= style()->pixelMetric(QStyle::PM_MenuButtonIndicator, &option, this);
|
|
|
|
}
|
2014-12-31 23:54:52 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QFileInfo const pathInfo(myPath);
|
2017-04-19 12:04:45 +00:00
|
|
|
option.text = myPath.isEmpty() ? tr("(None)") : (pathInfo.fileName().isEmpty() ? myPath : pathInfo.fileName());
|
|
|
|
option.text = fontMetrics().elidedText(option.text, Qt::ElideMiddle, textWidth);
|
2014-12-31 23:54:52 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
painter.drawComplexControl(QStyle::CC_ToolButton, option);
|
2014-12-31 23:54:52 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void PathButton::onClicked()
|
2014-12-31 21:00:34 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QFileDialog* dialog = new QFileDialog(window(), effectiveTitle());
|
|
|
|
dialog->setFileMode(isDirMode() ? QFileDialog::Directory : QFileDialog::ExistingFile);
|
|
|
|
|
|
|
|
if (isDirMode())
|
|
|
|
{
|
|
|
|
dialog->setOption(QFileDialog::ShowDirsOnly);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!myNameFilter.isEmpty())
|
2015-04-15 00:08:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
dialog->setNameFilter(myNameFilter);
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QFileInfo const pathInfo(myPath);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (!myPath.isEmpty() && pathInfo.exists())
|
|
|
|
{
|
|
|
|
if (pathInfo.isDir())
|
2015-04-15 00:08:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
dialog->setDirectory(pathInfo.absoluteFilePath());
|
2015-04-15 00:08:24 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2015-04-15 00:08:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
dialog->setDirectory(pathInfo.absolutePath());
|
|
|
|
dialog->selectFile(pathInfo.fileName());
|
2015-04-15 00:08:24 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-31 21:00:34 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
connect(dialog, SIGNAL(fileSelected(QString)), this, SLOT(onFileSelected(QString)));
|
2014-12-31 21:00:34 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
dialog->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
dialog->open();
|
2014-12-31 21:00:34 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void PathButton::onFileSelected(QString const& path)
|
2014-12-31 21:00:34 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!path.isEmpty())
|
|
|
|
{
|
|
|
|
setPath(path);
|
|
|
|
}
|
2014-12-31 21:00:34 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void PathButton::updateAppearance()
|
2014-12-31 21:00:34 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QFileInfo const pathInfo(myPath);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int const iconSize(style()->pixelMetric(QStyle::PM_SmallIconSize));
|
|
|
|
QFileIconProvider const iconProvider;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
QIcon icon;
|
2014-12-31 21:00:34 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!myPath.isEmpty() && pathInfo.exists())
|
|
|
|
{
|
|
|
|
icon = iconProvider.icon(myPath);
|
|
|
|
}
|
2014-12-31 21:00:34 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (icon.isNull())
|
|
|
|
{
|
|
|
|
icon = iconProvider.icon(isDirMode() ? QFileIconProvider::Folder : QFileIconProvider::File);
|
|
|
|
}
|
2014-12-31 21:00:34 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
setIconSize(QSize(iconSize, iconSize));
|
|
|
|
setIcon(icon);
|
|
|
|
setToolTip(myPath == text() ? QString() : myPath);
|
2014-12-31 23:54:52 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
update();
|
2014-12-31 21:00:34 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool PathButton::isDirMode() const
|
2014-12-31 21:00:34 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return myMode == DirectoryMode;
|
2014-12-31 21:00:34 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QString PathButton::effectiveTitle() const
|
2014-12-31 21:00:34 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!myTitle.isEmpty())
|
|
|
|
{
|
|
|
|
return myTitle;
|
|
|
|
}
|
2014-12-31 21:00:34 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return isDirMode() ? tr("Select Folder") : tr("Select File");
|
2014-12-31 21:00:34 +00:00
|
|
|
}
|