diff --git a/src/Lib/Ass/Line.cc b/src/Lib/Ass/Line.cc index ef6a9d0ad482c8cab146f689d64644e5b42020b1..7fabdbace9fdd01450f41f49c1f295b91ff5e88b 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 aeac0724c13bde021c65f5b769c3428bebe620bb..7463dad6692230fe9627ff83f21d4f32290f1862 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 e8950a17a600474368a4c8ea65617ad2aadc2aca..bf44811cc3bd82528efdd034b55dc605504cc405 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 62848968d141b05383622262d6c91738273a8086..8f7f0a8e6ed2a2cb232f5d0769bb31b891855e42 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 031bf9944b4023df1088bf41ca307c7e7e29d136..2872b4de1184ca003ebc57ed7ba3122813329716 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 9919bc63a5bf7b1b57b6bc1d38e5c28c8d1b59d9..54c58c660067f55270af7fa92d3ede42074823ae 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;