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  MythUIShape *barshape = dynamic_cast<MythUIShape *>(cut);
131  MythUIImage *barimage = dynamic_cast<MythUIImage *>(cut);
132  MythUIShape *leftshape = dynamic_cast<MythUIShape *>(cuttoleft);
133  MythUIImage *leftimage = dynamic_cast<MythUIImage *>(cuttoleft);
134  MythUIShape *rightshape = dynamic_cast<MythUIShape *>(cuttoright);
135  MythUIImage *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  MythUIShape *shape = dynamic_cast<MythUIShape *>(add);
206  MythUIImage *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  MythUIShape *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  MythUIImage *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  MythUIEditBar *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  MythUIEditBar *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 }
QList< QPair< float, float > > m_regions
Definition: mythuieditbar.h:48
MythUIType * GetNew(MythUIShape *shape, MythUIImage *image)
void SetRedraw(void)
Definition: mythuitype.cpp:295
void DeleteChild(const QString &name)
Delete a named child of this UIType.
Definition: mythuitype.cpp:147
Image widget, displays a single image or multiple images in sequence.
Definition: mythuiimage.h:97
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...
void AddBar(MythUIShape *shape, MythUIImage *image, const QRect &area)
QList< MythUIType * > m_images
Definition: mythuieditbar.h:50
void CreateCopy(MythUIType *parent) override
Copy the state of this widget to the one given, it must be of the same type.
void ClearImages(void)
The base class on which all widgets and screens are based.
Definition: mythuitype.h:63
void SetPosition(int x, int y)
Convenience method, calls SetPosition(const MythPoint&) Override that instead to change functionality...
Definition: mythuitype.cpp:519
MythUIEditBar(MythUIType *parent, const QString &name)
Definition: mythuieditbar.h:19
void CopyFrom(MythUIType *base) override
Copy this widgets state from another.
void CopyFrom(MythUIType *base) override
Copy this widgets state from another.
virtual void SetVisible(bool visible)
bool MoveToTop(void)
void AddMark(MythUIShape *shape, MythUIImage *image, int start, bool left)
virtual MythRect GetArea(void) const
If the object has a minimum area defined, return it, other wise return the default area.
Definition: mythuitype.cpp:863
void AddRegion(double start, double end)
void Finalize(void) override
Perform any post-xml parsing initialisation tasks.
void SetCropRect(int x, int y, int width, int height)
Definition: mythuishape.cpp:31
A widget for rendering primitive shapes and lines.
Definition: mythuishape.h:21
const char * name
Definition: ParseText.cpp:328
float m_editPosition
Definition: mythuieditbar.h:46
QList< QPair< float, float > > m_invregions
Definition: mythuieditbar.h:49
virtual void CopyFrom(MythUIType *base)
Copy this widgets state from another.
void SetTotal(double total)
void Display(void)
void ReleaseImages(void)
MythUIType * GetChild(const QString &name) const
Get a named child of this UIType.
Definition: mythuitype.cpp:132
void SetEditPosition(double position)
void ClearRegions(void)
void CopyFrom(MythUIType *base) override
Copy this widgets state from another.
A narrow purpose widget used to represent cut positions and regions when editing a video.
Definition: mythuieditbar.h:16
void CalcInverseRegions(void)