diff --git a/avogadro/qtgui/CMakeLists.txt b/avogadro/qtgui/CMakeLists.txt index c49b73396..f968fad40 100644 --- a/avogadro/qtgui/CMakeLists.txt +++ b/avogadro/qtgui/CMakeLists.txt @@ -57,6 +57,7 @@ avogadro_headers(QtGui persistentbond.h pluginlayermanager.h pythonscript.h + richtextdelegate.h rwmolecule.h sceneplugin.h scenepluginmodel.h @@ -85,6 +86,7 @@ target_sources(QtGui PRIVATE interfacewidget.cpp jsonwidget.cpp layermodel.cpp + richtextdelegate.cpp rwlayermanager.cpp meshgenerator.cpp molecule.cpp diff --git a/avogadro/qtgui/richtextdelegate.cpp b/avogadro/qtgui/richtextdelegate.cpp new file mode 100644 index 000000000..365b9b50b --- /dev/null +++ b/avogadro/qtgui/richtextdelegate.cpp @@ -0,0 +1,88 @@ +/****************************************************************************** + + This source file is part of the Avogadro project. + + Copyright 2015 Marcus Johansson + + 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 + : 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 diff --git a/avogadro/qtplugins/symmetry/richtextdelegate.h b/avogadro/qtgui/richtextdelegate.h similarity index 88% rename from avogadro/qtplugins/symmetry/richtextdelegate.h rename to avogadro/qtgui/richtextdelegate.h index 59ecaa54e..403e3cb7b 100644 --- a/avogadro/qtplugins/symmetry/richtextdelegate.h +++ b/avogadro/qtgui/richtextdelegate.h @@ -22,21 +22,22 @@ #include namespace Avogadro { -namespace QtPlugins { +namespace QtGui { class RichTextDelegate : public QStyledItemDelegate { Q_OBJECT public: - RichTextDelegate(QObject* parent = nullptr) - : QStyledItemDelegate(parent){}; + RichTextDelegate(QObject* parent = nullptr); + 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 diff --git a/avogadro/qtplugins/symmetry/CMakeLists.txt b/avogadro/qtplugins/symmetry/CMakeLists.txt index ea66c2fad..f33c721e6 100644 --- a/avogadro/qtplugins/symmetry/CMakeLists.txt +++ b/avogadro/qtplugins/symmetry/CMakeLists.txt @@ -8,7 +8,6 @@ set(symmetry_srcs symmetry.cpp symmetrywidget.cpp operationstablemodel.cpp - richtextdelegate.cpp symmetryutil.cpp ) @@ -35,4 +34,3 @@ avogadro_plugin(SymmetryScene target_link_libraries(Symmetry PRIVATE ${LIBMSYM_LIBRARIES}) target_link_libraries(SymmetryScene PRIVATE Avogadro::Rendering) - diff --git a/avogadro/qtplugins/symmetry/richtextdelegate.cpp b/avogadro/qtplugins/symmetry/richtextdelegate.cpp deleted file mode 100644 index a8fa646b6..000000000 --- a/avogadro/qtplugins/symmetry/richtextdelegate.cpp +++ /dev/null @@ -1,51 +0,0 @@ -/****************************************************************************** - - This source file is part of the Avogadro project. - - Copyright 2015 Marcus Johansson - - 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::QtPlugins { - -QSize RichTextDelegate::sizeHint(const QStyleOptionViewItem& o, - const QModelIndex& index) const -{ - QStyleOptionViewItemV4 ov4 = o; - initStyleOption(&ov4, index); - QTextDocument doc; - doc.setHtml(ov4.text); - doc.setTextWidth(ov4.rect.width()); - return QSize(doc.idealWidth(), doc.size().height()); -} - -void RichTextDelegate::paint(QPainter* p, const QStyleOptionViewItem& o, - const QModelIndex& index) const -{ - QStyleOptionViewItemV4 ov4 = o; - initStyleOption(&ov4, index); - - p->save(); - - QTextDocument doc; - doc.setHtml(ov4.text); - - ov4.text = ""; - ov4.widget->style()->drawControl(QStyle::CE_ItemViewItem, &ov4, p); - - p->translate(ov4.rect.left(), ov4.rect.top()); - QRect clip(0, 0, ov4.rect.width(), ov4.rect.height()); - doc.drawContents(p, clip); - p->restore(); -} -} diff --git a/avogadro/qtplugins/symmetry/symmetrywidget.cpp b/avogadro/qtplugins/symmetry/symmetrywidget.cpp index 15d824cc9..c3a6500d3 100644 --- a/avogadro/qtplugins/symmetry/symmetrywidget.cpp +++ b/avogadro/qtplugins/symmetry/symmetrywidget.cpp @@ -4,17 +4,18 @@ ******************************************************************************/ #include "symmetrywidget.h" -#include "richtextdelegate.h" #include "symmetryutil.h" #include "ui_symmetrywidget.h" #include +#include #include #include using Avogadro::QtGui::Molecule; +using Avogadro::QtGui::RichTextDelegate; using namespace msym; using namespace Avogadro::QtPlugins::SymmetryUtil; @@ -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); @@ -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( @@ -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) @@ -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(); @@ -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; @@ -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(generator - m_sg), - Qt::UserRole); + child->setData(static_cast(generator - m_sg), Qt::UserRole); parent->appendRow(child); } } @@ -400,4 +389,4 @@ msym_thresholds_t* SymmetryWidget::getThresholds() const return thresholds; } -} // namespace Avogadro +} // namespace Avogadro::QtPlugins