From abcd785ed218ffa55f8dc3064b1c173689c98e0c Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Tue, 13 Jul 2021 23:59:11 +0200
Subject: [PATCH] UI: Add the IsComment column

---
 src/Lib/Ass/Line.cc                   |  9 ++++++--
 src/Lib/Ass/Line.hh                   |  2 ++
 src/UI/DocumentViews/AssLinesModel.cc | 31 +++++++++++++++++++++++----
 src/UI/DocumentViews/AssLinesModel.hh |  4 +++-
 src/UI/DocumentViews/AssLinesView.cc  | 12 +++++++++++
 src/UI/DocumentViews/AssLinesView.hh  |  3 +++
 6 files changed, 54 insertions(+), 7 deletions(-)

diff --git a/src/Lib/Ass/Line.cc b/src/Lib/Ass/Line.cc
index ef6a9d0a..7fabdbac 100644
--- a/src/Lib/Ass/Line.cc
+++ b/src/Lib/Ass/Line.cc
@@ -25,8 +25,7 @@ Line::Line(AssFactory *const factory, const QString &lineString)
     };
 
     static const QString lineHeader = "Dialogue: ";
-    if (!lineString.startsWith(lineHeader))
-        throw std::runtime_error(("invalid event line header: " + lineString).toStdString());
+    isComment                       = !lineString.startsWith(lineHeader);
 
     const QString lineContent = lineString.mid(lineString.indexOf(": ") + 2 /* strlen ": " */);
     QStringList contentList   = lineContent.split(",", Qt::KeepEmptyParts, Qt::CaseInsensitive);
@@ -130,3 +129,9 @@ Line::getContent() const noexcept
 {
     return content;
 }
+
+bool
+Line::getIsComment() const noexcept
+{
+    return isComment;
+}
diff --git a/src/Lib/Ass/Line.hh b/src/Lib/Ass/Line.hh
index aeac0724..7463dad6 100644
--- a/src/Lib/Ass/Line.hh
+++ b/src/Lib/Ass/Line.hh
@@ -16,6 +16,7 @@ private:
     quint64 start{ 0 };
     quint64 end{ 0 };
     int layer{ 0 };
+    bool isComment{ true };
 
     QVector<Syl> content{};
     StyleProperties styleProperties{};
@@ -37,6 +38,7 @@ public:
     void setStart(quint64 s) noexcept;
     void setEnd(quint64 s) noexcept;
     quint64 getDuration() const noexcept;
+    bool getIsComment() const noexcept;
 
     StyleProperties getStyleProperties() const noexcept;
     StyleWeakPtr getStyle() const noexcept;
diff --git a/src/UI/DocumentViews/AssLinesModel.cc b/src/UI/DocumentViews/AssLinesModel.cc
index e8950a17..bf44811c 100644
--- a/src/UI/DocumentViews/AssLinesModel.cc
+++ b/src/UI/DocumentViews/AssLinesModel.cc
@@ -27,6 +27,14 @@ AssLinesModel::Item::getLineText() const noexcept
     return ret;
 }
 
+bool
+AssLinesModel::Item::getIsComment() const noexcept
+{
+    if (auto ptr = line.lock())
+        return ptr->getIsComment();
+    return false;
+}
+
 QString
 AssLinesModel::Item::getLineStyle() const noexcept
 {
@@ -57,13 +65,28 @@ AssLinesModel::data(const QModelIndex &index, int role) const noexcept
     if (!index.isValid() || index.column() >= Utils::toUnderlying(Item::Field::TotalFieldCount))
         return QVariant();
 
-    const Item *line = static_cast<const Item *>(index.internalPointer());
+    const Item *line         = static_cast<const Item *>(index.internalPointer());
+    const Item::Field column = static_cast<Item::Field>(index.column());
 
-    if (Qt::DisplayRole == role) {
-        switch (static_cast<Item::Field>(index.column())) {
+    // Check state only for the IsComment
+    if (role == Qt::CheckStateRole && column == Item::Field::IsComment) {
+        return line->getIsComment() ? Qt::Checked : Qt::Unchecked;
+    }
+
+    // Is comment is centered
+    else if (role == Qt::TextAlignmentRole && column == Item::Field::IsComment) {
+        return Qt::AlignCenter;
+    }
+
+    // Display role
+    else if (Qt::DisplayRole == role) {
+        switch (column) {
         case Item::Field::Style:
             return line->getLineStyle();
 
+        case Item::Field::IsComment:
+            return QVariant();
+
         case Item::Field::Text:
             return line->getLineText();
 
@@ -144,5 +167,5 @@ Qt::ItemFlags
 AssLinesModel::flags(const QModelIndex &index) const noexcept
 {
     [[maybe_unused]] const Item *item = static_cast<const Item *>(index.internalPointer());
-    return Qt::ItemIsSelectable | QAbstractItemModel::flags(index);
+    return Qt::ItemNeverHasChildren | Qt::ItemIsSelectable | QAbstractItemModel::flags(index);
 }
diff --git a/src/UI/DocumentViews/AssLinesModel.hh b/src/UI/DocumentViews/AssLinesModel.hh
index 62848968..8f7f0a8e 100644
--- a/src/UI/DocumentViews/AssLinesModel.hh
+++ b/src/UI/DocumentViews/AssLinesModel.hh
@@ -20,6 +20,7 @@ private:
         ~Item() noexcept = default;
 
         enum class Field : int {
+            IsComment,
             Style,
             Text,
 
@@ -27,6 +28,7 @@ private:
             TotalFieldCount,
         };
 
+        bool getIsComment() const noexcept;
         quint64 getLineNumber() const noexcept;
         QString getLineText() const noexcept;
         QString getLineStyle() const noexcept;
@@ -35,7 +37,7 @@ private:
         Ass::LineWeakPtr line;
     };
 
-    static inline const QStringList headers{ "Actor", "Text" };
+    static inline const QStringList headers{ "", "Actor", "Text" };
 
 public:
     explicit AssLinesModel(const QVector<Ass::LinePtr> &) noexcept;
diff --git a/src/UI/DocumentViews/AssLinesView.cc b/src/UI/DocumentViews/AssLinesView.cc
index 031bf994..2872b4de 100644
--- a/src/UI/DocumentViews/AssLinesView.cc
+++ b/src/UI/DocumentViews/AssLinesView.cc
@@ -86,3 +86,15 @@ AssLinesView::Delegate::onItemEntered(const QModelIndex &index) noexcept
 {
     hoveredRow = index.row();
 }
+
+void
+AssLinesView::Delegate::initStyleOption(QStyleOptionViewItem *option,
+                                        const QModelIndex &index) const noexcept
+{
+    QStyledItemDelegate::initStyleOption(option, index);
+
+    // The first column is the IsComment column
+    // XXX This is hardcoded
+    if (index.column() == 0)
+        option->displayAlignment = Qt::AlignCenter;
+}
diff --git a/src/UI/DocumentViews/AssLinesView.hh b/src/UI/DocumentViews/AssLinesView.hh
index 9919bc63..54c58c66 100644
--- a/src/UI/DocumentViews/AssLinesView.hh
+++ b/src/UI/DocumentViews/AssLinesView.hh
@@ -25,6 +25,9 @@ private:
         Delegate(AssLinesView *, QWidget *parent = nullptr) noexcept;
         ~Delegate() noexcept = default;
 
+        void initStyleOption(QStyleOptionViewItem *option,
+                             const QModelIndex &index) const noexcept override;
+
         void paint(QPainter *, const QStyleOptionViewItem &,
                    const QModelIndex &) const noexcept override;
         void onItemEntered(const QModelIndex &) noexcept;
-- 
GitLab