MythTV  master
mythuieditbar.cpp
Go to the documentation of this file.
1 
2 #include "mythuieditbar.h"
3 
4 // C++
5 #include <cmath>
6 
7 // MythBase
8 #include "mythlogging.h"
9 
10 // MythUI
11 #include "mythuishape.h"
12 #include "mythuiimage.h"
13 
15 {
16  ClearImages();
17 }
18 
19 void MythUIEditBar::SetTotal(double total)
20 {
21  if (total < 1.0)
22  return;
23 
24  bool changed = m_total != total;
25  m_total = total;
26 
27  if (changed)
28  Display();
29 }
30 
31 void MythUIEditBar::SetEditPosition(double position)
32 {
33  float newpos = position / m_total;
34 
35  if (newpos < 0.0F || newpos > 1.0F)
36  return;
37 
38  bool changed = m_editPosition != newpos;
39  m_editPosition = newpos;
40 
41  if (changed)
42  Display();
43 }
44 
45 void MythUIEditBar::AddRegion(double start, double end)
46 {
47  if (m_total >= end && end >= start && start >= 0.0)
48  m_regions.append(qMakePair((float)(start / m_total),
49  (float)(end / m_total)));
50 }
51 
52 void MythUIEditBar::SetTotal(long long total)
53 {
54  SetTotal((double)total);
55 }
56 
57 void MythUIEditBar::SetEditPosition(long long position)
58 {
59  SetEditPosition((double)position);
60 }
61 
62 void MythUIEditBar::AddRegion(long long start, long long end)
63 {
64  AddRegion((double)start, (double)end);
65 }
66 
68 {
69  m_regions.clear();
70 }
71 
73 {
74  QRect keeparea = QRect();
75  QRect cutarea = QRect();
76  MythUIType *position = GetChild("position");
77  MythUIType *keep = GetChild("keep");
78  MythUIType *cut = GetChild("cut");
79  MythUIType *cuttoleft = GetChild("cuttoleft");
80  MythUIType *cuttoright = GetChild("cuttoright");
81  MythUIType *keeptoleft = GetChild("keeptoleft");
82  MythUIType *keeptoright = GetChild("keeptoright");
83 
84  if (position)
85  position->SetVisible(false);
86 
87  if (keep)
88  {
89  keep->SetVisible(false);
90  keeparea = keep->GetArea();
91  }
92 
93  if (cut)
94  {
95  cut->SetVisible(false);
96  cutarea = cut->GetArea();
97  }
98 
99  if (cuttoleft)
100  cuttoleft->SetVisible(false);
101 
102  if (cuttoright)
103  cuttoright->SetVisible(false);
104 
105  if (keeptoleft)
106  keeptoleft->SetVisible(false);
107 
108  if (keeptoright)
109  keeptoright->SetVisible(false);
110 
111  if (position && keeparea.isValid())
112  {
113  int offset = position->GetArea().width() / 2;
114  int newx = lroundf((float)keeparea.width() * m_editPosition);
115  int newy = position->GetArea().top();
116  position->SetPosition(newx - offset, newy);
117  position->SetVisible(true);
118  }
119 
120  ClearImages();
121 
122  if (m_regions.isEmpty())
123  {
124  if (keep)
125  keep->SetVisible(true);
126 
127  return;
128  }
129 
130  auto *barshape = dynamic_cast<MythUIShape *>(cut);
131  auto *barimage = dynamic_cast<MythUIImage *>(cut);
132  auto *leftshape = dynamic_cast<MythUIShape *>(cuttoleft);
133  auto *leftimage = dynamic_cast<MythUIImage *>(cuttoleft);
134  auto *rightshape = dynamic_cast<MythUIShape *>(cuttoright);
135  auto *rightimage = dynamic_cast<MythUIImage *>(cuttoright);
136 
137  QListIterator<QPair<float, float> > regions(m_regions);
138 
139  while (regions.hasNext() && cutarea.isValid())
140  {
141  QPair<float, float> region = regions.next();
142  int left = lroundf(region.first * cutarea.width());
143  int right = lroundf(region.second * cutarea.width());
144 
145  if (left >= right)
146  right = left + 1;
147 
148  if (cut)
149  {
150  AddBar(barshape, barimage, QRect(left, cutarea.top(), right - left,
151  cutarea.height()));
152  }
153 
154  if (cuttoleft && (region.second < 1.0F))
155  AddMark(leftshape, leftimage, right, true);
156 
157  if (cuttoright && (region.first > 0.0F))
158  AddMark(rightshape, rightimage, left, false);
159  }
160 
162 
163  barshape = dynamic_cast<MythUIShape *>(keep);
164  barimage = dynamic_cast<MythUIImage *>(keep);
165  leftshape = dynamic_cast<MythUIShape *>(keeptoleft);
166  leftimage = dynamic_cast<MythUIImage *>(keeptoleft);
167  rightshape = dynamic_cast<MythUIShape *>(keeptoright);
168  rightimage = dynamic_cast<MythUIImage *>(keeptoright);
169 
170  QListIterator<QPair<float, float> > regions2(m_invregions);
171 
172  while (regions2.hasNext() && keeparea.isValid())
173  {
174  QPair<float, float> region = regions2.next();
175  int left = lroundf(region.first * keeparea.width());
176  int right = lroundf(region.second * keeparea.width());
177 
178  if (left >= right)
179  right = left + 1;
180 
181  if (keep)
182  {
183  AddBar(barshape, barimage, QRect(left, keeparea.top(), right - left,
184  keeparea.height()));
185  }
186 
187  if (keeptoleft && (region.second < 1.0F))
188  AddMark(leftshape, leftimage, right, true);
189 
190  if (keeptoright && (region.first > 0.0F))
191  AddMark(rightshape, rightimage, left, false);
192  }
193 
194  if (position)
195  position->MoveToTop();
196 }
197 
199  const QRect &area)
200 {
201  MythUIType *add = GetNew(_shape, _image);
202 
203  if (add)
204  {
205  auto *shape = dynamic_cast<MythUIShape *>(add);
206  auto *image = dynamic_cast<MythUIImage *>(add);
207 
208  if (shape)
209  shape->SetCropRect(area.left(), area.top(), area.width(), area.height());
210 
211  if (image)
212  image->SetCropRect(area.left(), area.top(), area.width(), area.height());
213 
214  add->SetPosition(area.left(), area.top());
215  }
216 }
217 
219  int start, bool left)
220 {
221  MythUIType *add = GetNew(shape, image);
222 
223  if (add)
224  {
225  if (left)
226  start -= add->GetArea().width();
227 
228  add->SetPosition(start, add->GetArea().top());
229  }
230 }
231 
233 {
234  QString name = QString("editbarimage_%1").arg(m_images.size());
235 
236  if (shape)
237  {
238  auto *newshape = new MythUIShape(this, name);
239 
240  if (newshape)
241  {
242  newshape->CopyFrom(shape);
243  newshape->SetVisible(true);
244  m_images.append(newshape);
245  return newshape;
246  }
247  }
248  else if (image)
249  {
250  auto *newimage = new MythUIImage(this, name);
251 
252  if (newimage)
253  {
254  newimage->CopyFrom(image);
255  newimage->SetVisible(true);
256  m_images.append(newimage);
257  return newimage;
258  }
259  }
260 
261  return nullptr;
262 }
263 
265 {
266  m_invregions.clear();
267 
268  bool first = true;
269  float start = 0.0F;
270  QListIterator<QPair<float, float> > regions(m_regions);
271 
272  while (regions.hasNext())
273  {
274  QPair<float, float> region = regions.next();
275 
276  if (first)
277  {
278  if (region.first > 0.0F)
279  m_invregions.append(qMakePair(start, region.first));
280 
281  start = region.second;
282  first = false;
283  }
284  else
285  {
286  m_invregions.append(qMakePair(start, region.first));
287  start = region.second;
288  }
289  }
290 
291  if (start < 1.0F)
292  m_invregions.append(qMakePair(start, 1.0F));
293 }
294 
296 {
297  while (!m_images.empty())
298  DeleteChild(m_images.takeFirst());
299  SetRedraw();
300 }
301 
306 {
307  auto *editbar = dynamic_cast<MythUIEditBar *>(base);
308 
309  if (!editbar)
310  return;
311 
312  m_editPosition = editbar->m_editPosition;
313 
314  QListIterator<QPair<float, float> > it(m_regions);
315 
316  while (it.hasNext())
317  editbar->m_regions.append(it.next());
318 
319  MythUIType::CopyFrom(base);
320 }
321 
326 {
327  auto *editbar = new MythUIEditBar(parent, objectName());
328  editbar->CopyFrom(this);
329 }
330 
335 {
336  MythUIType *position = GetChild("position");
337 
338  if (position)
339  position->MoveToTop();
340 }
MythUIEditBar::m_total
double m_total
Definition: mythuieditbar.h:47
MythUIImage
Image widget, displays a single image or multiple images in sequence.
Definition: mythuiimage.h:98
MythUIType::DeleteChild
void DeleteChild(const QString &name)
Delete a named child of this UIType.
Definition: mythuitype.cpp:145
MythUIEditBar::Display
void Display(void)
Definition: mythuieditbar.cpp:72
MythUIEditBar::ReleaseImages
void ReleaseImages(void)
Definition: mythuieditbar.cpp:14
MythUIType::GetChild
MythUIType * GetChild(const QString &name) const
Get a named child of this UIType.
Definition: mythuitype.cpp:130
MythUIEditBar
A narrow purpose widget used to represent cut positions and regions when editing a video.
Definition: mythuieditbar.h:17
MythUIEditBar::GetNew
MythUIType * GetNew(MythUIShape *shape, MythUIImage *image)
Definition: mythuieditbar.cpp:232
MythUIEditBar::SetTotal
void SetTotal(double total)
Definition: mythuieditbar.cpp:19
MythUIEditBar::AddMark
void AddMark(MythUIShape *shape, MythUIImage *image, int start, bool left)
Definition: mythuieditbar.cpp:218
mythuieditbar.h
mythuiimage.h
MythUIType::GetArea
virtual MythRect GetArea(void) const
If the object has a minimum area defined, return it, other wise return the default area.
Definition: mythuitype.cpp:869
MythUIEditBar::CreateCopy
void CreateCopy(MythUIType *parent) override
Copy the state of this widget to the one given, it must be of the same type.
Definition: mythuieditbar.cpp:325
MythUIImage::SetCropRect
void SetCropRect(int x, int y, int width, int height)
Crop the image using the given rectangle, useful for removing unsightly edges from imported images or...
Definition: mythuiimage.cpp:948
mythlogging.h
MythUIType::SetPosition
void SetPosition(int x, int y)
Convenience method, calls SetPosition(const MythPoint&) Override that instead to change functionality...
Definition: mythuitype.cpp:517
MythUIEditBar::AddRegion
void AddRegion(double start, double end)
Definition: mythuieditbar.cpp:45
MythUIEditBar::CalcInverseRegions
void CalcInverseRegions(void)
Definition: mythuieditbar.cpp:264
MythUIEditBar::m_invregions
QList< QPair< float, float > > m_invregions
Definition: mythuieditbar.h:49
MythUIType::CopyFrom
virtual void CopyFrom(MythUIType *base)
Copy this widgets state from another.
Definition: mythuitype.cpp:1152
MythUIEditBar::MythUIEditBar
MythUIEditBar(MythUIType *parent, const QString &name)
Definition: mythuieditbar.h:19
MythUIType::MoveToTop
bool MoveToTop(void)
Definition: mythuitype.cpp:1355
MythUIType
The base class on which all widgets and screens are based.
Definition: mythuitype.h:64
MythUIShape
A widget for rendering primitive shapes and lines.
Definition: mythuishape.h:22
MythUIEditBar::ClearRegions
void ClearRegions(void)
Definition: mythuieditbar.cpp:67
MythUIEditBar::ClearImages
void ClearImages(void)
Definition: mythuieditbar.cpp:295
MythUIEditBar::SetEditPosition
void SetEditPosition(double position)
Definition: mythuieditbar.cpp:31
MythUIEditBar::Finalize
void Finalize(void) override
Perform any post-xml parsing initialisation tasks.
Definition: mythuieditbar.cpp:334
MythUIType::SetVisible
virtual void SetVisible(bool visible)
Definition: mythuitype.cpp:1085
MythUIEditBar::m_images
QList< MythUIType * > m_images
Definition: mythuieditbar.h:50
MythUIEditBar::m_editPosition
float m_editPosition
Definition: mythuieditbar.h:46
MythUIEditBar::m_regions
QList< QPair< float, float > > m_regions
Definition: mythuieditbar.h:48
MythUIEditBar::AddBar
void AddBar(MythUIShape *shape, MythUIImage *image, const QRect &area)
Definition: mythuieditbar.cpp:198
MythUIType::SetRedraw
void SetRedraw(void)
Definition: mythuitype.cpp:293
mythuishape.h
MythUIEditBar::CopyFrom
void CopyFrom(MythUIType *base) override
Copy this widgets state from another.
Definition: mythuieditbar.cpp:305