summaryrefslogtreecommitdiff
path: root/src/main-view/DictTableDelegatePainter.cpp
diff options
context:
space:
mode:
authorJedidiah Barber <contact@jedbarber.id.au>2021-07-14 11:49:10 +1200
committerJedidiah Barber <contact@jedbarber.id.au>2021-07-14 11:49:10 +1200
commitd24f813f3f2a05c112e803e4256b53535895fc98 (patch)
tree601e6ae9a1cd44bcfdcf91739a5ca36aedd827c9 /src/main-view/DictTableDelegatePainter.cpp
Initial mirror commitHEADmaster
Diffstat (limited to 'src/main-view/DictTableDelegatePainter.cpp')
-rw-r--r--src/main-view/DictTableDelegatePainter.cpp61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/main-view/DictTableDelegatePainter.cpp b/src/main-view/DictTableDelegatePainter.cpp
new file mode 100644
index 0000000..2714bdc
--- /dev/null
+++ b/src/main-view/DictTableDelegatePainter.cpp
@@ -0,0 +1,61 @@
+#include "DictTableDelegatePainter.h"
+
+DictTableDelegatePainter::DictTableDelegatePainter(QPainter* painter,
+ const QRect& contentRect, const QFontMetrics& fontMetrics):
+ painter(painter), contentRect(contentRect), fontMetrics(fontMetrics)
+{
+ initLoopParams();
+}
+
+void DictTableDelegatePainter::initLoopParams()
+{
+ offset = 0;
+ textFitstoRect = true;
+}
+
+void DictTableDelegatePainter::startDrawing()
+{
+ painter->save();
+ painter->setClipRect(contentRect);
+ initLoopParams();
+}
+
+void DictTableDelegatePainter::endDrawing()
+{
+ painter->restore();
+}
+
+void DictTableDelegatePainter::drawText(const QString& text)
+{
+ if(!textFitstoRect)
+ return;
+ QString elidedText = getElidedText(text);
+ textFitstoRect = elidedText == text;
+ painter->drawText(contentRect.translated(offset, 0),
+ elidedText, getTextOption());
+ offset += fontMetrics.width(elidedText);
+}
+
+void DictTableDelegatePainter::drawImage(const QString& filePath)
+{
+ if(!textFitstoRect)
+ return;
+ QPixmap image(filePath);
+ if(image.isNull())
+ image = QPixmap(":/images/broken-image.png");
+ image = image.scaledToHeight(ThumbnailSize, Qt::SmoothTransformation);
+ painter->drawPixmap(contentRect.topLeft() + QPoint(offset, 0), image);
+ offset += image.width();
+}
+
+QString DictTableDelegatePainter::getElidedText(const QString& text)
+{
+ return fontMetrics.elidedText(text, Qt::ElideRight, contentRect.width() - offset);
+}
+
+QTextOption DictTableDelegatePainter::getTextOption()
+{
+ QTextOption textOption(Qt::AlignVCenter);
+ textOption.setWrapMode(QTextOption::NoWrap);
+ return textOption;
+}