Skip to content

Commit

Permalink
Add a preview panel when PNG images are present for molecules
Browse files Browse the repository at this point in the history
Signed-off-by: Geoff Hutchison <[email protected]>
  • Loading branch information
ghutchis committed Sep 20, 2023
1 parent 88f4665 commit 2da2eed
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
36 changes: 33 additions & 3 deletions avogadro/qtplugins/insertfragment/insertfragmentdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class InsertFragmentDialog::Private

~Private()
{
delete model; // proxy is handled through the model

delete model; // proxy is handled through the model
}
};

Expand All @@ -60,6 +60,8 @@ InsertFragmentDialog::InsertFragmentDialog(QWidget* aParent, QString directory,
{
setWindowFlags(Qt::Dialog | Qt::Tool);
m_ui->setupUi(this);
// to start, hide the preview
m_ui->preview->hide();

m_implementation->currentFileName.clear();
if (directory.contains(QLatin1String("crystals")))
Expand Down Expand Up @@ -112,6 +114,13 @@ InsertFragmentDialog::InsertFragmentDialog(QWidget* aParent, QString directory,
m_implementation->model->setReadOnly(true);
QModelIndex rootIndex = m_implementation->model->setRootPath(m_directory);

QStringList filters;
if (m_implementation->crystalFiles)
filters << "*.cif";
else
filters << "*.cjson";
m_implementation->model->setNameFilters(filters);

m_implementation->proxy = new SortFilterTreeProxyModel(this);
m_implementation->proxy->setSourceModel(m_implementation->model);
m_implementation->proxy->setSortLocaleAware(true); // important for files
Expand Down Expand Up @@ -144,6 +153,9 @@ InsertFragmentDialog::InsertFragmentDialog(QWidget* aParent, QString directory,

connect(m_ui->filterLineEdit, SIGNAL(textChanged(const QString&)), this,
SLOT(filterTextChanged(const QString&)));

connect(m_ui->directoryTreeView, SIGNAL(clicked(const QModelIndex&)), this,
SLOT(clicked(const QModelIndex&)));
}

InsertFragmentDialog::~InsertFragmentDialog()
Expand All @@ -169,6 +181,24 @@ QString InsertFragmentDialog::fileName()
return selected.first().data(QFileSystemModel::FilePathRole).toString();
}

void InsertFragmentDialog::clicked(const QModelIndex& selected)
{
if (m_implementation == nullptr || m_implementation->model == nullptr)
return;

// Remember to map to the source model
QString fileName = selected.data(QFileSystemModel::FilePathRole).toString();
QFileInfo info(fileName);
if (!info.isDir()) {
// get the PNG name
QString pngName = info.absolutePath() + '/' + info.baseName() + ".png";

m_ui->preview->setIcon(QIcon(pngName));
m_ui->preview->show();
} else
m_ui->preview->hide();
}

void InsertFragmentDialog::refresh()
{
m_ui->directoryTreeView->update();
Expand Down Expand Up @@ -201,4 +231,4 @@ void InsertFragmentDialog::activated()
emit performInsert(currentFileName, m_implementation->crystalFiles);
}

} // namespace Avogadro
} // namespace Avogadro::QtPlugins
8 changes: 5 additions & 3 deletions avogadro/qtplugins/insertfragment/insertfragmentdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,20 @@ class InsertFragmentDialog : public QDialog
public Q_SLOTS:
void refresh();

void filterTextChanged(const QString &);
void filterTextChanged(const QString&);

void activated();

void clicked(const QModelIndex& selected);

Q_SIGNALS:
void performInsert(const QString &fileName, bool crystal);
void performInsert(const QString& fileName, bool crystal);

private:
Ui::InsertFragmentDialog* m_ui;

class Private;
Private *m_implementation;
Private* m_implementation;
};

} // namespace QtPlugins
Expand Down
19 changes: 18 additions & 1 deletion avogadro/qtplugins/insertfragment/insertfragmentdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,24 @@
</layout>
</item>
<item>
<widget class="QTreeView" name="directoryTreeView"/>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QTreeView" name="directoryTreeView"/>
</item>
<item row="0" column="1">
<widget class="QToolButton" name="preview">
<property name="text">
<string/>
</property>
<property name="iconSize">
<size>
<width>128</width>
<height>128</height>
</size>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="Line" name="line_2">
Expand Down

0 comments on commit 2da2eed

Please sign in to comment.