Blame view

ModelView.cpp 5.78 KB
3f2992b2c   bostx   V1.0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
  #include <QVBoxLayout>
  
  #include "ModelView.h"
  #include "Shot.h"
  #include "VideoFrame.h"
  
  #include <QDebug>
  
  ModelView::ModelView(ProjectModel *project, int nVignettes, QWidget *parent)
    : QWidget(parent),
      m_currEpisode(0),
      m_project(project),
      m_nVignettes(nVignettes),
      m_playerPaused(true)
  {
    m_treeView = new QTreeView;
    m_treeView->expandAll();
  
    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(m_treeView);
    setLayout(layout);
  
    m_treeView->setModel(m_project);
    m_selection = m_treeView->selectionModel();
  
    connect(m_selection, SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
  	  this, SLOT(updatePlayer(const QModelIndex &, const QModelIndex &)));
  }
  
  void ModelView::adjust()
  {
    m_treeView->resizeColumnToContents(0);
    m_treeView->resizeColumnToContents(1);
  }
  
  void ModelView::initPlayer()
  {
    QModelIndex seasonIndex = m_project->index(0, 0);
    updatePlayer(seasonIndex, QModelIndex());
  }
  
  /////////////////////////////////////////////
  // slot called when clicking on model view //
  /////////////////////////////////////////////
  
  void ModelView::updatePlayer(const QModelIndex &current, const QModelIndex &previous)
  {
    Q_UNUSED(previous);
  
    Shot *shot;
  
    // scroll to selected segment
    m_treeView->scrollTo(current, QAbstractItemView::PositionAtCenter);
  
    // selected segment
    Segment *segment = static_cast<Segment *>(current.internalPointer());
  
    // emit signals when encountering shot
    if ((shot = dynamic_cast<Shot *>(segment)))
      emit currentShot(genPositionList(segment));
  
    updateCurrentEpisode(segment);
    emit setPlayer(m_currEpisode->getFName(), m_currEpisode->getResolution());
    emit setPlayerPosition(segment->getPosition());
  
    if (m_playerPaused) {
     
      qint64 position = segment->getPosition();
      VideoFrame *vFrame;
      int i;
  
      // retrieve current video frame
      while (!(vFrame = dynamic_cast<VideoFrame *>(segment))) {
  
        // closest segment index to current player position
        i = segment->childIndexFromPosition(position);
  
        // select possible children
        segment = segment->child(i);
      }
  
      emit displaySubtitle(vFrame->getSub());
    }
  }
  
  ///////////////////////////////////////////
  // slot called when player state changes //
  ///////////////////////////////////////////
  
  void ModelView::playerPaused(bool pause)
  {
    m_playerPaused = pause;
  }
  
  //////////////////////////////////////////////
  // slot called when player position changes //
  //////////////////////////////////////////////
  
  void ModelView::positionChanged(qint64 position)
  {
    int i(-1);
    QModelIndex index;
    QModelIndex siblingIndex;
    VideoFrame *vFrame;
  
    m_selection->clearSelection();
  
    Segment *segment = m_currEpisode;
  
    // retrieve segment to select
    int depth = 0;
    while (depth++ < m_depth) {
  
      // closest segment index to current player position
      i = segment->childIndexFromPosition(position);
  
      // select possible children
      segment = segment->child(i);
    }
  
    // model indexes to select
    index = m_project->indexFromSegment(segment);
    siblingIndex = m_project->sibling(i, 1, index);
    
    // segment selection
    m_selection->setCurrentIndex(index, QItemSelectionModel::Select);
    m_selection->setCurrentIndex(siblingIndex, QItemSelectionModel::Select);
  
    // retrieve current video frame
    while (!(vFrame = dynamic_cast<VideoFrame *>(segment))) {
  
      // closest segment index to current player position
      i = segment->childIndexFromPosition(position);
  
      // select possible children
      segment = segment->child(i);
    }
  
    emit displaySubtitle(vFrame->getSub());
  }
  
  ///////////////////////////////////
  // slot called when pressing key //
  ///////////////////////////////////
  
  void ModelView::keyPressEvent(QKeyEvent *event)
  {
    Segment *segment = static_cast<Segment *>(m_selection->currentIndex().internalPointer());
  
    if (event->key() == Qt::Key_Return && dynamic_cast<VideoFrame *>(segment))
      emit insertShot(segment);
    else if (event->key() == Qt::Key_Backspace && dynamic_cast<Shot *>(segment))
      emit removeShot(segment);
    else if (event->key() == Qt::Key_Return && dynamic_cast<Shot *>(segment))
      emit editSimShots(segment);
  }
  
  ///////////////////////////////////////////////////
  // slots called when entering in annotation mode //
  ///////////////////////////////////////////////////
  
  void ModelView::initShotAnnot(bool checked)
  {
    Segment *segment = static_cast<Segment *>(m_selection->currentIndex().internalPointer());
  
    if (checked)
      emit initShotLevel(segment);
  }
  
  ///////////////
  // accessors //
  ///////////////
  
  QString ModelView::getCurrentEpisodeFName() const
  {
    return m_currEpisode->getFName();
  }
  
  /////////////////////
  // private methods //
  /////////////////////
  
  void ModelView::updateCurrentEpisode(Segment *segment)
  {
    Episode *episode = 0;
  
    // looking for corresponding episode
    Segment *tmp = segment;
  
    // searching among parent segments
    while (tmp != 0 && !(episode = dynamic_cast<Episode *>(tmp)))
      tmp = tmp->parent();
  
    // if not found, searching among child segments
    if (!tmp) {
      tmp = segment;
      while (tmp != 0 && !(episode = dynamic_cast<Episode *>(tmp)))
        tmp = tmp->child(0);
    }
  
    if (episode != m_currEpisode) {
      m_currEpisode = episode;
    }
  }
  
  QList<qint64> ModelView::genPositionList(Segment *segment)
  {
    QList<qint64> positionList;
    qint64 segmentPosition;
  
    int iFirst = segment->row() - m_nVignettes / 2;
    int iLast = segment->row() + m_nVignettes / 2;
  
    for (int i(iFirst); i <= iLast; i++) {
      if (i >= 0 && i < segment->parent()->childCount()) {
        segmentPosition = segment->parent()->child(i)->getPosition();
        positionList.push_back(segmentPosition);
      }
      else
        positionList.push_back(-1);
    }
  
    return positionList;
  }
  
  ///////////////
  // modifiers //
  ///////////////
  
  void ModelView::setDepth(int depth)
  {
    m_depth = depth - 2;
    m_treeView->expandToDepth(m_depth);
    adjust();
  }