| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "insertfragmentdialog.h" |
| 7 |
#include "sortfiltertreeproxymodel.h" |
| 8 |
#include "ui_insertfragmentdialog.h" |
| 9 |
|
| 10 |
#include <avogadro/qtgui/utilities.h> |
| 11 |
|
| 12 |
#include <QtCore/QSettings> |
| 13 |
|
| 14 |
#include <QFileSystemModel> |
| 15 |
#include <QtCore/QDir> |
| 16 |
#include <QtCore/QSortFilterProxyModel> |
| 17 |
#include <QtCore/QStandardPaths> |
| 18 |
#include <QtWidgets/QFileDialog> |
| 19 |
#include <QtWidgets/QMessageBox> |
| 20 |
|
| 21 |
#include <QCloseEvent> |
| 22 |
|
| 23 |
#include <QtCore/QDebug> |
| 24 |
|
| 25 |
namespace Avogadro::QtGui { |
| 26 |
|
| 27 |
class InsertFragmentDialog::Private |
| 28 |
{ |
| 29 |
public: |
| 30 |
QFileSystemModel* model; |
| 31 |
SortFilterTreeProxyModel* proxy; |
| 32 |
QModelIndex proxyRoot; |
| 33 |
|
| 34 |
QString currentFileName; |
| 35 |
bool crystalFiles; // are we inserting crystals (i.e., don't center) |
| 36 |
|
| 37 |
~Private() |
| 38 |
{ |
| 39 |
|
| 40 |
delete model; // proxy is handled through the model |
| 41 |
} |
| 42 |
}; |
| 43 |
|
| 44 |
InsertFragmentDialog::InsertFragmentDialog(QWidget* aParent, QString directory) |
| 45 |
: QDialog(aParent), m_ui(new Ui::InsertFragmentDialog), |
| 46 |
m_implementation(new Private) |
| 47 |
{ |
| 48 |
setWindowFlags(Qt::Dialog | Qt::Tool); |
| 49 |
m_ui->setupUi(this); |
| 50 |
// to start, hide the preview |
| 51 |
m_ui->preview->hide(); |
| 52 |
|
| 53 |
m_implementation->currentFileName.clear(); |
| 54 |
if (directory.contains(QLatin1String("crystals"))) |
| 55 |
m_implementation->crystalFiles = true; |
| 56 |
else |
| 57 |
m_implementation->crystalFiles = false; |
| 58 |
|
| 59 |
// we check for the downloaded version first |
| 60 |
QStringList dirs; |
| 61 |
QStringList stdPaths = |
| 62 |
QStandardPaths::standardLocations(QStandardPaths::AppLocalDataLocation); |
| 63 |
foreach (const QString& dirStr, stdPaths) { |
| 64 |
QString path = dirStr + "/data"; |
| 65 |
dirs << path; // we'll check if these exist below |
| 66 |
} |
| 67 |
|
| 68 |
// add in paths relative to the binary (e.g. for development) |
| 69 |
dirs << QCoreApplication::applicationDirPath() + "/../" + |
| 70 |
QtGui::Utilities::dataDirectory() + "/avogadro2"; |
| 71 |
|
| 72 |
#ifdef Q_WS_X11 |
| 73 |
dirs << QString(INSTALL_PREFIX) + "/share/avogadro2/"; |
| 74 |
#else |
| 75 |
// Mac and Windows use relative path from application location |
| 76 |
dirs << QCoreApplication::applicationDirPath() + "/../share/avogadro2"; |
| 77 |
#endif |
| 78 |
|
| 79 |
QString m_directory; |
| 80 |
QDir dir; |
| 81 |
|
| 82 |
foreach (const QString& dirStr, dirs) { |
| 83 |
qDebug() << "Checking for " << directory << " data in" << dirStr; |
| 84 |
QDir testdir(dirStr + '/' + directory); |
| 85 |
if (testdir.exists() && testdir.isReadable()) { |
| 86 |
m_directory = testdir.absolutePath(); |
| 87 |
break; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
if (m_directory.isEmpty()) { |
| 92 |
// Can't really do anything! |
| 93 |
m_ui->directoryTreeView->setEnabled(false); |
| 94 |
m_ui->insertFragmentButton->setEnabled(false); |
| 95 |
m_ui->filterLineEdit->setEnabled(false); |
| 96 |
|
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
m_implementation->model = new QFileSystemModel(this); |
| 101 |
m_implementation->model->setReadOnly(true); |
| 102 |
QModelIndex rootIndex = m_implementation->model->setRootPath(m_directory); |
| 103 |
|
| 104 |
QStringList filters; |
| 105 |
if (m_implementation->crystalFiles) |
| 106 |
filters << "*.cif"; |
| 107 |
else |
| 108 |
filters << "*.cjson" |
| 109 |
<< "*.cml"; |
| 110 |
m_implementation->model->setNameFilters(filters); |
| 111 |
|
| 112 |
m_implementation->proxy = new SortFilterTreeProxyModel(this); |
| 113 |
m_implementation->proxy->setSourceModel(m_implementation->model); |
| 114 |
m_implementation->proxy->setSortLocaleAware(true); // important for files |
| 115 |
// map from the root path to the proxy index |
| 116 |
m_implementation->proxyRoot = |
| 117 |
m_implementation->proxy->mapFromSource(rootIndex); |
| 118 |
// Our custom class needs this to prevent becoming rootless |
| 119 |
m_implementation->proxy->setSourceRoot(rootIndex); |
| 120 |
|
| 121 |
m_ui->directoryTreeView->setModel(m_implementation->proxy); |
| 122 |
// remember to map from the source to the proxy index |
| 123 |
m_ui->directoryTreeView->setRootIndex(m_implementation->proxyRoot); |
| 124 |
|
| 125 |
// hide everything but the filename |
| 126 |
for (int i = 1; i < m_implementation->model->columnCount(); ++i) |
| 127 |
m_ui->directoryTreeView->hideColumn(i); |
| 128 |
|
| 129 |
m_ui->directoryTreeView->setSelectionMode(QAbstractItemView::SingleSelection); |
| 130 |
m_ui->directoryTreeView->setSelectionBehavior(QAbstractItemView::SelectRows); |
| 131 |
m_ui->directoryTreeView->setUniformRowHeights(true); |
| 132 |
|
| 133 |
connect(m_ui->insertFragmentButton, SIGNAL(clicked(bool)), this, |
| 134 |
SLOT(activated())); |
| 135 |
|
| 136 |
connect(m_ui->directoryTreeView, SIGNAL(doubleClicked(const QModelIndex)), |
| 137 |
this, SLOT(activated())); |
| 138 |
|
| 139 |
connect(m_ui->directoryTreeView, SIGNAL(activated(const QModelIndex)), this, |
| 140 |
SLOT(activated())); |
| 141 |
|
| 142 |
connect(m_ui->filterLineEdit, SIGNAL(textChanged(const QString&)), this, |
| 143 |
SLOT(filterTextChanged(const QString&))); |
| 144 |
|
| 145 |
connect(m_ui->directoryTreeView->selectionModel(), |
| 146 |
SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)), this, |
| 147 |
SLOT(currentChanged(const QModelIndex&, const QModelIndex&))); |
| 148 |
} |
| 149 |
|
| 150 |
InsertFragmentDialog::~InsertFragmentDialog() |
| 151 |
{ |
| 152 |
delete m_ui; |
| 153 |
delete m_implementation; |
| 154 |
} |
| 155 |
|
| 156 |
QString InsertFragmentDialog::fileName() |
| 157 |
{ |
| 158 |
if (m_implementation == nullptr || m_implementation->model == nullptr) |
| 159 |
return QString(); |
| 160 |
|
| 161 |
// The selected model index is in the proxy |
| 162 |
QModelIndexList selected = |
| 163 |
m_ui->directoryTreeView->selectionModel()->selectedIndexes(); |
| 164 |
|
| 165 |
if (selected.isEmpty()) { |
| 166 |
return QString(); |
| 167 |
} |
| 168 |
|
| 169 |
// Remember to map to the source model |
| 170 |
return selected.first().data(QFileSystemModel::FilePathRole).toString(); |
| 171 |
} |
| 172 |
|
| 173 |
void InsertFragmentDialog::currentChanged(const QModelIndex& selected, |
| 174 |
const QModelIndex& deselected) |
| 175 |
{ |
| 176 |
Q_UNUSED(deselected) |
| 177 |
if (m_implementation == nullptr || m_implementation->model == nullptr) |
| 178 |
return; |
| 179 |
|
| 180 |
// Remember to map to the source model |
| 181 |
QString fileName = selected.data(QFileSystemModel::FilePathRole).toString(); |
| 182 |
QFileInfo info(fileName); |
| 183 |
if (!info.isDir()) { |
| 184 |
// get the image name -- default to svg |
| 185 |
QString imgName = info.absolutePath() + '/' + info.baseName() + ".svg"; |
| 186 |
|
| 187 |
// check if the svg exists, if not try png |
| 188 |
if (!QFile::exists(imgName)) { |
| 189 |
imgName = info.absolutePath() + '/' + info.baseName() + ".png"; |
| 190 |
} |
| 191 |
|
| 192 |
m_ui->preview->setIcon(QIcon(imgName)); |
| 193 |
m_ui->preview->show(); |
| 194 |
} else |
| 195 |
m_ui->preview->hide(); |
| 196 |
} |
| 197 |
|
| 198 |
void InsertFragmentDialog::refresh() |
| 199 |
{ |
| 200 |
m_ui->directoryTreeView->update(); |
| 201 |
} |
| 202 |
|
| 203 |
void InsertFragmentDialog::filterTextChanged(const QString& newFilter) |
| 204 |
{ |
| 205 |
if (!m_implementation || !m_implementation->proxy) |
| 206 |
return; // no dialog or proxy model to set |
| 207 |
|
| 208 |
// Allow things like "ti" to match "Ti" etc. |
| 209 |
QRegularExpression reg(newFilter, QRegularExpression::CaseInsensitiveOption); |
| 210 |
m_implementation->proxy->setFilterRegularExpression(reg); |
| 211 |
|
| 212 |
if (!newFilter.isEmpty()) { |
| 213 |
// user interface niceness -- show any file match |
| 214 |
m_ui->directoryTreeView->expandToDepth(2); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
void InsertFragmentDialog::activated() |
| 219 |
{ |
| 220 |
QString currentFileName = fileName(); |
| 221 |
|
| 222 |
// check to see if it's an actual file and not a directory |
| 223 |
if (currentFileName.isEmpty() || !QFileInfo(currentFileName).isFile()) { |
| 224 |
return; |
| 225 |
} |
| 226 |
|
| 227 |
emit performInsert(currentFileName, m_implementation->crystalFiles); |
| 228 |
} |
| 229 |
|
| 230 |
} // namespace Avogadro::QtGui |