File indexing completed on 2024-05-26 04:59:34

0001 /*
0002     SPDX-FileCopyrightText: 2007-2009 Sergio Pistone <sergio_pistone@yahoo.com.ar>
0003     SPDX-FileCopyrightText: 2010-2022 Mladen Milinkovic <max@smoothware.net>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #ifndef LINESWIDGET_H
0009 #define LINESWIDGET_H
0010 
0011 #include "core/subtitle.h"
0012 #include "gui/treeview/linesmodel.h"
0013 #include "gui/treeview/treeview.h"
0014 
0015 #include <QPen>
0016 
0017 namespace SubtitleComposer {
0018 class LinesItemDelegate;
0019 
0020 class LinesWidget : public TreeView
0021 {
0022     Q_OBJECT
0023 
0024 public:
0025     explicit LinesWidget(QWidget *parent);
0026     virtual ~LinesWidget();
0027 
0028     bool showingContextMenu();
0029 
0030     SubtitleLine * currentLine() const;
0031     int currentLineIndex() const;
0032 
0033     int firstSelectedIndex() const;
0034     int lastSelectedIndex() const;
0035     bool selectionHasMultipleRanges() const;
0036     RangeList selectionRanges() const;
0037     RangeList targetRanges(int target) const;
0038 
0039     inline LinesModel * model() const { return static_cast<LinesModel *>(TreeView::model()); }
0040     inline bool scrollFollowsModel() const { return m_scrollFollowsModel; }
0041 
0042     inline bool isEditing() { return m_inlineEditor != nullptr; }
0043 
0044     void loadConfig();
0045     void saveConfig();
0046 
0047     bool eventFilter(QObject *object, QEvent *event) override;
0048 
0049 public slots:
0050     void setSubtitle(Subtitle *subtitle = 0);
0051     void setTranslationMode(bool enabled);
0052 
0053     void setCurrentLine(SubtitleLine *line, bool clearSelection = true);
0054     void setPlayingLine(SubtitleLine *line);
0055 
0056     void editCurrentLineInPlace(bool primaryText = true);
0057 
0058 signals:
0059     void currentLineChanged(SubtitleLine *line);
0060     void lineDoubleClicked(SubtitleLine *line);
0061 
0062 protected slots:
0063     void closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint) override;
0064 
0065 private:
0066     void contextMenuEvent(QContextMenuEvent *e) override;
0067     void mouseDoubleClickEvent(QMouseEvent *e) override;
0068 
0069     static void drawHorizontalDotLine(QPainter *painter, int x1, int x2, int y);
0070     static void drawVerticalDotLine(QPainter *painter, int x, int y1, int y2);
0071     void updateHeader();
0072 
0073     void drawRow(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
0074 
0075 private slots:
0076     void onCurrentRowChanged();
0077 
0078 private:
0079     bool m_scrollFollowsModel;
0080     bool m_translationMode;
0081     bool m_showingContextMenu;
0082     QPen m_gridPen;
0083 
0084     LinesItemDelegate *m_itemsDelegate;
0085     QWidget *m_inlineEditor;
0086 
0087     friend class LinesWidgetScrollToModelDetacher;
0088     friend class LinesModel;
0089     friend class LinesItemDelegate;
0090 };
0091 
0092 class LinesWidgetScrollToModelDetacher
0093 {
0094 public:
0095     inline LinesWidgetScrollToModelDetacher(LinesWidget &w) : m_linesWidget(w) { m_linesWidget.m_scrollFollowsModel = false; }
0096     inline ~LinesWidgetScrollToModelDetacher() { m_linesWidget.m_scrollFollowsModel = true; }
0097 
0098 private:
0099     LinesWidget &m_linesWidget;
0100 };
0101 }
0102 #endif