Skip to content

Commit

Permalink
Move RichTextDelegate to qtgui for use in other tools
Browse files Browse the repository at this point in the history
Signed-off-by: Geoff Hutchison <[email protected]>
  • Loading branch information
ghutchis committed Oct 20, 2024
1 parent 0b02b7c commit 8566f2c
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 90 deletions.
2 changes: 2 additions & 0 deletions avogadro/qtgui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ avogadro_headers(QtGui
persistentbond.h
pluginlayermanager.h
pythonscript.h
richtextdelegate.h
rwmolecule.h
sceneplugin.h
scenepluginmodel.h
Expand Down Expand Up @@ -85,6 +86,7 @@ target_sources(QtGui PRIVATE
interfacewidget.cpp
jsonwidget.cpp
layermodel.cpp
richtextdelegate.cpp
rwlayermanager.cpp
meshgenerator.cpp
molecule.cpp
Expand Down
88 changes: 88 additions & 0 deletions avogadro/qtgui/richtextdelegate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/******************************************************************************
This source file is part of the Avogadro project.
Copyright 2015 Marcus Johansson <[email protected]>
This source code is released under the New BSD License, (the "License").
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************/

#include "richtextdelegate.h"

namespace Avogadro::QtGui {

// See for example
// https://gist.github.com/jniemann66/dbc298b35a840bf3f1a2206ea6284c7b
// and https://stackoverflow.com/a/66412883/131896

RichTextDelegate::RichTextDelegate(QObject* parent)
: QStyledItemDelegate(parent)
{
}

QSize RichTextDelegate::sizeHint(const QStyleOptionViewItem& o,
const QModelIndex& index) const
{
if (o.text.isEmpty()) {
// This is nothing this function is supposed to handle
return QStyledItemDelegate::sizeHint(o, index);
}

QStyleOptionViewItem ov = o;
initStyleOption(&ov, index);

QTextDocument doc;
doc.setHtml(ov.text);
doc.setTextWidth(ov.rect.width());
doc.setDefaultFont(ov.font);
doc.setDocumentMargin(1);

return QSize(doc.idealWidth(), doc.size().height());
}

void RichTextDelegate::paint(QPainter* p, const QStyleOptionViewItem& o,
const QModelIndex& index) const
{
if (o.text.isEmpty()) {
// no need to do anything if the text is empty
QStyledItemDelegate::paint(p, o, index);

return;
}

QStyleOptionViewItem ov = o;
initStyleOption(&ov, index);

p->save();

QTextDocument doc;
doc.setHtml(ov.text);

QTextOption textOption;
textOption.setWrapMode(ov.features & QStyleOptionViewItem::WrapText
? QTextOption::WordWrap

Check notice on line 70 in avogadro/qtgui/richtextdelegate.cpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

avogadro/qtgui/richtextdelegate.cpp#L70

Clarify calculation precedence for '&' and '?'.
: QTextOption::ManualWrap);
textOption.setTextDirection(ov.direction);
doc.setDefaultTextOption(textOption);
doc.setDefaultFont(ov.font);
doc.setDocumentMargin(1);
doc.setTextWidth(ov.rect.width());
doc.adjustSize();

ov.text = "";
ov.widget->style()->drawControl(QStyle::CE_ItemViewItem, &ov, p);

p->translate(ov.rect.left(), ov.rect.top());
QRect clip(0, 0, ov.rect.width(), ov.rect.height());
doc.drawContents(p, clip);
p->restore();
}

} // namespace Avogadro::QtGui
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,22 @@
#include <QTextDocument>

namespace Avogadro {
namespace QtPlugins {
namespace QtGui {

class RichTextDelegate : public QStyledItemDelegate
{
Q_OBJECT

public:
RichTextDelegate(QObject* parent = nullptr)
: QStyledItemDelegate(parent){};
RichTextDelegate(QObject* parent = nullptr);

Check notice on line 32 in avogadro/qtgui/richtextdelegate.h

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

avogadro/qtgui/richtextdelegate.h#L32

Class 'RichTextDelegate' has a constructor with 1 argument that is not explicit.

QSize sizeHint(const QStyleOptionViewItem& o,
const QModelIndex& index) const override;
void paint(QPainter* p, const QStyleOptionViewItem& o,
const QModelIndex& index) const override;
};
}
}

#endif // AVOGADRO_QTPLUGINS_RICHTEXTDELEGATE_H
} // namespace QtGui
} // namespace Avogadro

#endif // AVOGADRO_QTGUI_RICHTEXTDELEGATE_H
2 changes: 0 additions & 2 deletions avogadro/qtplugins/symmetry/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ set(symmetry_srcs
symmetry.cpp
symmetrywidget.cpp
operationstablemodel.cpp
richtextdelegate.cpp
symmetryutil.cpp
)

Expand All @@ -35,4 +34,3 @@ avogadro_plugin(SymmetryScene

target_link_libraries(Symmetry PRIVATE ${LIBMSYM_LIBRARIES})
target_link_libraries(SymmetryScene PRIVATE Avogadro::Rendering)

51 changes: 0 additions & 51 deletions avogadro/qtplugins/symmetry/richtextdelegate.cpp

This file was deleted.

51 changes: 20 additions & 31 deletions avogadro/qtplugins/symmetry/symmetrywidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
******************************************************************************/

#include "symmetrywidget.h"
#include "richtextdelegate.h"
#include "symmetryutil.h"
#include "ui_symmetrywidget.h"

#include <avogadro/qtgui/molecule.h>
#include <avogadro/qtgui/richtextdelegate.h>

#include <QtCore/QDebug>

#include <QtWidgets/QPlainTextEdit>

using Avogadro::QtGui::Molecule;
using Avogadro::QtGui::RichTextDelegate;

using namespace msym;
using namespace Avogadro::QtPlugins::SymmetryUtil;
Expand Down Expand Up @@ -62,18 +63,11 @@ msym_thresholds_t sloppy_thresholds = {
};

SymmetryWidget::SymmetryWidget(QWidget* parent_)
: QWidget(parent_)
, m_ui(new Ui::SymmetryWidget)
, m_molecule(nullptr)
, m_equivalenceTreeModel(new QStandardItemModel(this))
, m_operationsTableModel(new OperationsTableModel(this))
, m_subgroupsTreeModel(new QStandardItemModel(this))
, m_es(nullptr)
, m_sops(nullptr)
, m_sg(nullptr)
, m_sopsl(0)
, m_sgl(0)
, m_radius(0.0)
: QWidget(parent_), m_ui(new Ui::SymmetryWidget), m_molecule(nullptr),
m_equivalenceTreeModel(new QStandardItemModel(this)),
m_operationsTableModel(new OperationsTableModel(this)),
m_subgroupsTreeModel(new QStandardItemModel(this)), m_es(nullptr),
m_sops(nullptr), m_sg(nullptr), m_sopsl(0), m_sgl(0), m_radius(0.0)
{
setWindowFlags(Qt::Dialog);
m_ui->setupUi(this);
Expand All @@ -87,10 +81,9 @@ SymmetryWidget::SymmetryWidget(QWidget* parent_)
m_ui->subgroupsTree->setModel(m_subgroupsTreeModel);
m_ui->subgroupsTree->setItemDelegateForColumn(0, new RichTextDelegate(this));

connect(
m_ui->detectSymmetryButton, SIGNAL(clicked()), SIGNAL(detectSymmetry()));
connect(m_ui->symmetrizeMoleculeButton,
SIGNAL(clicked()),
connect(m_ui->detectSymmetryButton, SIGNAL(clicked()),
SIGNAL(detectSymmetry()));
connect(m_ui->symmetrizeMoleculeButton, SIGNAL(clicked()),
SIGNAL(symmetrizeMolecule()));

connect(
Expand Down Expand Up @@ -138,8 +131,7 @@ void SymmetryWidget::moleculeChanged(unsigned int changes)
}

void SymmetryWidget::operationsSelectionChanged(
const QItemSelection& selected,
const QItemSelection& deselected)
const QItemSelection& selected, const QItemSelection& deselected)
{

if (!m_molecule)
Expand Down Expand Up @@ -240,24 +232,23 @@ void SymmetryWidget::subgroupsSelectionChanged(const QItemSelection& selected,
QModelIndex left = m_operationsTableModel->index(row, 0);
QModelIndex right = m_operationsTableModel->index(
row, m_operationsTableModel->columnCount(left) - 1);
//if (!left.isValid() || !right.isValid())
// qDebug() << "invalid index " << j;
// if (!left.isValid() || !right.isValid())
// qDebug() << "invalid index " << j;
QItemSelection sel(left, right);

selection.merge(sel, QItemSelectionModel::Select);
}

QModelIndexList tmp = selection.indexes();
//foreach (QModelIndex j, tmp) {
// qDebug() << "selecting " << j.row() << " " << j.column();
//}
// foreach (QModelIndex j, tmp) {
// qDebug() << "selecting " << j.row() << " " << j.column();
// }

selectionModel->select(selection, QItemSelectionModel::ClearAndSelect);
}

void SymmetryWidget::equivalenceSelectionChanged(
const QItemSelection& selected,
const QItemSelection& deselected)
const QItemSelection& selected, const QItemSelection& deselected)
{
QModelIndex i =
m_ui->equivalenceTree->selectionModel()->selectedIndexes().first();
Expand Down Expand Up @@ -313,8 +304,7 @@ void SymmetryWidget::setPointGroupSymbol(QString pg)
}

void SymmetryWidget::setSymmetryOperations(
int sopsl,
const msym::msym_symmetry_operation_t* sops)
int sopsl, const msym::msym_symmetry_operation_t* sops)
{
m_sops = sops;
m_sopsl = sopsl;
Expand Down Expand Up @@ -373,8 +363,7 @@ void SymmetryWidget::setSubgroups(int sgl, const msym::msym_subgroup_t* sg)
auto* const child = new QStandardItem;
child->setText(pointGroupSymbol(generator->name));

child->setData(static_cast<int>(generator - m_sg),
Qt::UserRole);
child->setData(static_cast<int>(generator - m_sg), Qt::UserRole);
parent->appendRow(child);
}
}
Expand All @@ -400,4 +389,4 @@ msym_thresholds_t* SymmetryWidget::getThresholds() const
return thresholds;
}

} // namespace Avogadro
} // namespace Avogadro::QtPlugins

0 comments on commit 8566f2c

Please sign in to comment.