Warning, file /sdk/lokalize/src/noteeditor.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002   This file is part of Lokalize
0003 
0004   SPDX-FileCopyrightText: 2009-2014 Nick Shaforostoff <shafff@ukr.net>
0005   SPDX-FileCopyrightText: 2018-2019 Simon Depiets <sdepiets@gmail.com>
0006 
0007   SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0008 */
0009 
0010 #include "noteeditor.h"
0011 
0012 #include "lokalize_debug.h"
0013 
0014 #include "catalog.h"
0015 #include "cmd.h"
0016 #include "prefs_lokalize.h"
0017 
0018 #include <klocalizedstring.h>
0019 
0020 #include <QStringBuilder>
0021 #include <QBoxLayout>
0022 #include <QStackedLayout>
0023 #include <QLabel>
0024 #include <QLineEdit>
0025 #include <QPushButton>
0026 #include <QTextBrowser>
0027 #include <QDialogButtonBox>
0028 #include <QComboBox>
0029 #include <QCompleter>
0030 #include <QKeyEvent>
0031 #include <QStringListModel>
0032 
0033 void TextEdit::keyPressEvent(QKeyEvent* keyEvent)
0034 {
0035     if (keyEvent->modifiers()& Qt::ControlModifier
0036         && keyEvent->key() == Qt::Key_Return)
0037         Q_EMIT accepted();
0038     else if (keyEvent->key() == Qt::Key_Escape)
0039         Q_EMIT rejected();
0040     else
0041         QPlainTextEdit::keyPressEvent(keyEvent);
0042 }
0043 
0044 NoteEditor::NoteEditor(QWidget* parent)
0045     : QWidget(parent)
0046     , m_from(new QComboBox(this))
0047     , m_fromLabel(new QLabel(i18nc("@info:label", "From:"), this))
0048     , m_authors(new QStringListModel(this))
0049     , m_edit(new TextEdit(this))
0050     , m_idx(-1)
0051 {
0052     setToolTip(i18nc("@info:tooltip", "Save empty note to remove it"));
0053     m_from->setToolTip(i18nc("@info:tooltip", "Author of this note"));
0054     m_from->setEditable(true);
0055     m_from->setModel(m_authors);
0056     m_from->completer()->setModel(m_authors);
0057 
0058     QVBoxLayout* main = new QVBoxLayout(this);
0059     QHBoxLayout* prop = new QHBoxLayout;
0060     main->addLayout(prop);
0061     prop->addWidget(m_fromLabel);
0062     prop->addWidget(m_from, 42);
0063     main->addWidget(m_edit);
0064 
0065     QDialogButtonBox* box = new QDialogButtonBox(QDialogButtonBox::Save | QDialogButtonBox::Discard, this);
0066     box->button(QDialogButtonBox::Save)->setToolTip(i18n("Ctrl+Enter"));
0067     box->button(QDialogButtonBox::Discard)->setToolTip(i18n("Esc"));
0068 
0069     connect(m_edit, &TextEdit::accepted, this, &NoteEditor::accepted);
0070     connect(m_edit, &TextEdit::rejected, this, &NoteEditor::rejected);
0071     connect(box->button(QDialogButtonBox::Save), &QPushButton::clicked, this, &NoteEditor::accepted);
0072     connect(box->button(QDialogButtonBox::Discard), &QPushButton::clicked, this, &NoteEditor::rejected);
0073 
0074     main->addWidget(box);
0075 }
0076 
0077 void NoteEditor::setFromFieldVisible(bool v)
0078 {
0079     m_fromLabel->setVisible(v);
0080     m_from->setVisible(v);
0081 }
0082 
0083 Note NoteEditor::note()
0084 {
0085     m_note.content = m_edit->toPlainText();
0086     m_note.from = m_from->currentText();
0087     return m_note;
0088 }
0089 
0090 void NoteEditor::setNote(const Note& note, int idx)
0091 {
0092     m_note = note;
0093     m_edit->setPlainText(note.content);
0094     QString from = note.from;
0095     if (from.isEmpty()) from = Settings::authorName();
0096     m_from->setCurrentText(from);
0097     QStringList l = m_authors->stringList();
0098     if (!l.contains(from)) {
0099         l.append(from);
0100         m_authors->setStringList(l);
0101     }
0102     m_idx = idx;
0103     m_edit->setFocus();
0104 }
0105 
0106 void NoteEditor::setNoteAuthors(const QStringList& authors)
0107 {
0108     m_authors->setStringList(authors);
0109 }
0110 
0111 int displayNotes(QTextBrowser* browser, const QVector< Note >& notes, int active, bool multiple)
0112 {
0113     QTextCursor t = browser->textCursor();
0114     t.movePosition(QTextCursor::End);
0115     int realOffset = 0;
0116 
0117     static const QString BR = QStringLiteral("<br />");
0118     if (!notes.isEmpty()) {
0119         t.insertHtml(i18nc("@info XLIFF notes representation", "<b>Notes:</b>") + BR);
0120         int i = 0;
0121         for (const Note& note : notes) {
0122             if (!note.from.isEmpty())
0123                 t.insertHtml(QStringLiteral("<i>") + note.from + QStringLiteral(":</i> "));
0124 
0125             if (i == active)
0126                 realOffset = t.position();
0127             QString content = escapeWithLinks(note.content);
0128             if (!multiple && content.contains('\n')) content += '\n';
0129             content.replace('\n', BR);
0130             content += QString(QStringLiteral(" (<a href=\"note:/%1\">")).arg(i) + i18nc("link to edit note", "edit...") + QStringLiteral("</a>)<br />");
0131             t.insertHtml(content);
0132             i++;
0133         }
0134         if (multiple)
0135             t.insertHtml(QStringLiteral("<a href=\"note:/add\">") + i18nc("link to add a note", "Add...") + QStringLiteral("</a> "));
0136     } else
0137         browser->insertHtml(QStringLiteral("<a href=\"note:/add\">") + i18nc("link to add a note", "Add a note...") + QStringLiteral("</a> "));
0138 
0139     return realOffset;
0140 }
0141