summaryrefslogtreecommitdiff
path: root/src/main-view/RecordEditor.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main-view/RecordEditor.cpp')
-rw-r--r--src/main-view/RecordEditor.cpp122
1 files changed, 122 insertions, 0 deletions
diff --git a/src/main-view/RecordEditor.cpp b/src/main-view/RecordEditor.cpp
new file mode 100644
index 0000000..3f7a96c
--- /dev/null
+++ b/src/main-view/RecordEditor.cpp
@@ -0,0 +1,122 @@
+#include "RecordEditor.h"
+
+#include <QtDebug>
+
+const float RecordEditor::EditorWidthIncrease = 1.5;
+
+RecordEditor::RecordEditor(QWidget* parent, const QRect& cellRect) :
+ QTextEdit(parent), cellRect(cellRect),
+ enabledSizeUpdates(true)
+{
+ setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
+ setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+ setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+ connect( this, SIGNAL(textChanged()), SLOT(updateEditor()) );
+}
+
+void RecordEditor::startDrawing()
+{
+ clear();
+ enabledSizeUpdates = false;
+}
+
+void RecordEditor::endDrawing()
+{
+ enabledSizeUpdates = true;
+ updateEditor();
+}
+
+void RecordEditor::drawText(const QString& text)
+{
+ textCursor().insertText(text);
+}
+
+void RecordEditor::drawImage(const QString& filePath)
+{
+ QTextImageFormat format;
+ format.setVerticalAlignment(QTextCharFormat::AlignBaseline);
+ format.setName(filePath);
+ format.setHeight(ThumbnailSize);
+ textCursor().insertImage(format);
+}
+
+void RecordEditor::insertImage(int cursorPos, const QString& filePath)
+{
+ moveCursor(cursorPos);
+ drawImage(filePath);
+}
+
+void RecordEditor::moveCursor(int cursorPos)
+{
+ QTextCursor cursor = textCursor();
+ cursor.setPosition(cursorPos);
+ setTextCursor(cursor);
+}
+
+QString RecordEditor::getText() const
+{
+ QString text;
+ QTextBlock block = document()->begin();
+ while(block.isValid())
+ {
+ for(QTextBlock::iterator it = block.begin(); !it.atEnd(); it++)
+ {
+ QTextCharFormat format = it.fragment().charFormat();
+ if(format.isImageFormat())
+ text += QString("<img src=\"") + format.toImageFormat().name() + "\">";
+ else
+ text += it.fragment().text();
+ }
+ block = block.next();
+ }
+ return text;
+}
+
+QSize RecordEditor::sizeHint() const
+{
+ int width = getEditorWidth();
+ return QSize(width, getEditorHeight());
+}
+
+int RecordEditor::getEditorWidth() const
+{
+ int horMargins = contentsMargins().left() * 2;
+ int textWidth = cellRect.width() - horMargins;
+ document()->setTextWidth(textWidth);
+ if(textWrapped() && textWidth < MinWidth)
+ {
+ textWidth = MinWidth;
+ document()->setTextWidth(textWidth);
+ }
+ return textWidth + horMargins;
+}
+
+bool RecordEditor::textWrapped() const
+{
+ return document()->size().height() > cellRect.height();
+}
+
+int RecordEditor::getEditorHeight() const
+{
+ return document()->size().height() + contentsMargins().top() * 2;
+}
+
+void RecordEditor::updateEditor()
+{
+ if(!enabledSizeUpdates)
+ return;
+ adjustSize();
+ updatePos();
+}
+
+void RecordEditor::updatePos()
+{
+ QRect editorRect = cellRect;
+ editorRect.setSize(size());
+ QSize viewportSize = qobject_cast<QWidget*>(parent())->size();
+ if(editorRect.right() > viewportSize.width())
+ editorRect.moveRight(viewportSize.width());
+ if(editorRect.bottom() > viewportSize.height())
+ editorRect.moveBottom(viewportSize.height());
+ move(editorRect.topLeft());
+}