00001 #include "mythlogging.h"
00002 #include "mythuieditbar.h"
00003
00004 MythUIEditBar::MythUIEditBar(MythUIType *parent, const QString &name)
00005 : MythUIType(parent, name), m_position(0.0), m_total(1.0)
00006 {
00007 }
00008
00009 MythUIEditBar::~MythUIEditBar(void)
00010 {
00011 ReleaseImages();
00012 }
00013
00014 void MythUIEditBar::ReleaseImages(void)
00015 {
00016 ClearImages();
00017 }
00018
00019 void MythUIEditBar::SetTotal(double total)
00020 {
00021 if (total < 1.0)
00022 return;
00023
00024 bool changed = m_total != total;
00025 m_total = total;
00026
00027 if (changed)
00028 Display();
00029 }
00030
00031 void MythUIEditBar::SetPosition(double position)
00032 {
00033 float newpos = position / m_total;
00034
00035 if (newpos < 0.0f || newpos > 1.0f)
00036 return;
00037
00038 bool changed = m_position != newpos;
00039 m_position = newpos;
00040
00041 if (changed)
00042 Display();
00043 }
00044
00045 void MythUIEditBar::AddRegion(double start, double end)
00046 {
00047 if (m_total >= end && end >= start && start >= 0.0)
00048 m_regions.append(qMakePair((float)(start / m_total),
00049 (float)(end / m_total)));
00050 }
00051
00052 void MythUIEditBar::SetTotal(long long total)
00053 {
00054 SetTotal((double)total);
00055 }
00056
00057 void MythUIEditBar::SetPosition(long long position)
00058 {
00059 SetPosition((double)position);
00060 }
00061
00062 void MythUIEditBar::AddRegion(long long start, long long end)
00063 {
00064 AddRegion((double)start, (double)end);
00065 }
00066
00067 void MythUIEditBar::ClearRegions(void)
00068 {
00069 m_regions.clear();
00070 }
00071
00072 void MythUIEditBar::Display(void)
00073 {
00074 QRect keeparea = QRect();
00075 QRect cutarea = QRect();
00076 MythUIType *position = GetChild("position");
00077 MythUIType *keep = GetChild("keep");
00078 MythUIType *cut = GetChild("cut");
00079 MythUIType *cuttoleft = GetChild("cuttoleft");
00080 MythUIType *cuttoright = GetChild("cuttoright");
00081 MythUIType *keeptoleft = GetChild("keeptoleft");
00082 MythUIType *keeptoright = GetChild("keeptoright");
00083
00084 if (position)
00085 position->SetVisible(false);
00086
00087 if (keep)
00088 {
00089 keep->SetVisible(false);
00090 keeparea = keep->GetArea();
00091 }
00092
00093 if (cut)
00094 {
00095 cut->SetVisible(false);
00096 cutarea = cut->GetArea();
00097 }
00098
00099 if (cuttoleft)
00100 cuttoleft->SetVisible(false);
00101
00102 if (cuttoright)
00103 cuttoright->SetVisible(false);
00104
00105 if (keeptoleft)
00106 keeptoleft->SetVisible(false);
00107
00108 if (keeptoright)
00109 keeptoright->SetVisible(false);
00110
00111 if (position && keeparea.isValid())
00112 {
00113 int offset = position->GetArea().width() / 2;
00114 int newx = (int)(((float)keeparea.width() * m_position) + 0.5f);
00115 int newy = position->GetArea().top();
00116 position->SetPosition(newx - offset, newy);
00117 position->SetVisible(true);
00118 }
00119
00120 ClearImages();
00121
00122 if (!m_regions.size())
00123 {
00124 if (keep)
00125 keep->SetVisible(true);
00126
00127 return;
00128 }
00129
00130 MythUIShape *barshape = dynamic_cast<MythUIShape *>(cut);
00131 MythUIImage *barimage = dynamic_cast<MythUIImage *>(cut);
00132 MythUIShape *leftshape = dynamic_cast<MythUIShape *>(cuttoleft);
00133 MythUIImage *leftimage = dynamic_cast<MythUIImage *>(cuttoleft);
00134 MythUIShape *rightshape = dynamic_cast<MythUIShape *>(cuttoright);
00135 MythUIImage *rightimage = dynamic_cast<MythUIImage *>(cuttoright);
00136
00137 QListIterator<QPair<float, float> > regions(m_regions);
00138
00139 while (regions.hasNext() && cutarea.isValid())
00140 {
00141 QPair<float, float> region = regions.next();
00142 int left = (int)((region.first * cutarea.width()) + 0.5f);
00143 int right = (int)((region.second * cutarea.width()) + 0.5f);
00144
00145 if (left >= right)
00146 right = left + 1;
00147
00148 if (cut)
00149 {
00150 AddBar(barshape, barimage, QRect(left, cutarea.top(), right - left,
00151 cutarea.height()));
00152 }
00153
00154 if (cuttoleft && (region.second < 1.0f))
00155 AddMark(leftshape, leftimage, right, true);
00156
00157 if (cuttoright && (region.first > 0.0f))
00158 AddMark(rightshape, rightimage, left, false);
00159 }
00160
00161 CalcInverseRegions();
00162
00163 barshape = dynamic_cast<MythUIShape *>(keep);
00164 barimage = dynamic_cast<MythUIImage *>(keep);
00165 leftshape = dynamic_cast<MythUIShape *>(keeptoleft);
00166 leftimage = dynamic_cast<MythUIImage *>(keeptoleft);
00167 rightshape = dynamic_cast<MythUIShape *>(keeptoright);
00168 rightimage = dynamic_cast<MythUIImage *>(keeptoright);
00169
00170 QListIterator<QPair<float, float> > regions2(m_invregions);
00171
00172 while (regions2.hasNext() && keeparea.isValid())
00173 {
00174 QPair<float, float> region = regions2.next();
00175 int left = (int)((region.first * keeparea.width()) + 0.5f);
00176 int right = (int)((region.second * keeparea.width()) + 0.5f);
00177
00178 if (left >= right)
00179 right = left + 1;
00180
00181 if (keep)
00182 {
00183 AddBar(barshape, barimage, QRect(left, keeparea.top(), right - left,
00184 keeparea.height()));
00185 }
00186
00187 if (keeptoleft && (region.second < 1.0f))
00188 AddMark(leftshape, leftimage, right, true);
00189
00190 if (keeptoright && (region.first > 0.0f))
00191 AddMark(rightshape, rightimage, left, false);
00192 }
00193
00194 if (position)
00195 position->MoveToTop();
00196 }
00197
00198 void MythUIEditBar::AddBar(MythUIShape *shape, MythUIImage *image,
00199 const QRect &area)
00200 {
00201 MythUIType *add = GetNew(shape, image);
00202
00203 if (add)
00204 {
00205 MythUIShape *shape = dynamic_cast<MythUIShape *>(add);
00206 MythUIImage *image = dynamic_cast<MythUIImage *>(add);
00207
00208 if (shape)
00209 shape->SetCropRect(area.left(), area.top(), area.width(), area.height());
00210
00211 if (image)
00212 image->SetCropRect(area.left(), area.top(), area.width(), area.height());
00213
00214 add->SetPosition(area.left(), area.top());
00215 }
00216 }
00217
00218 void MythUIEditBar::AddMark(MythUIShape *shape, MythUIImage *image,
00219 int start, bool left)
00220 {
00221 MythUIType *add = GetNew(shape, image);
00222
00223 if (add)
00224 {
00225 if (left)
00226 start -= add->GetArea().width();
00227
00228 add->SetPosition(start, add->GetArea().top());
00229 }
00230 }
00231
00232 MythUIType *MythUIEditBar::GetNew(MythUIShape *shape, MythUIImage *image)
00233 {
00234 QString name = QString("editbarimage_%1").arg(m_images.size());
00235
00236 if (shape)
00237 {
00238 MythUIShape *newshape = new MythUIShape(this, name);
00239
00240 if (newshape)
00241 {
00242 newshape->CopyFrom(shape);
00243 newshape->SetVisible(true);
00244 m_images.append(newshape);
00245 return newshape;
00246 }
00247 }
00248 else if (image)
00249 {
00250 MythUIImage *newimage = new MythUIImage(this, name);
00251
00252 if (newimage)
00253 {
00254 newimage->CopyFrom(image);
00255 newimage->SetVisible(true);
00256 m_images.append(newimage);
00257 return newimage;
00258 }
00259 }
00260
00261 return NULL;
00262 }
00263
00264 void MythUIEditBar::CalcInverseRegions(void)
00265 {
00266 m_invregions.clear();
00267
00268 bool first = true;
00269 float start = 0.0f;
00270 QListIterator<QPair<float, float> > regions(m_regions);
00271
00272 while (regions.hasNext())
00273 {
00274 QPair<float, float> region = regions.next();
00275
00276 if (first)
00277 {
00278 if (region.first > 0.0f)
00279 m_invregions.append(qMakePair(start, region.first));
00280
00281 start = region.second;
00282 first = false;
00283 }
00284 else
00285 {
00286 m_invregions.append(qMakePair(start, region.first));
00287 start = region.second;
00288 }
00289 }
00290
00291 if (start < 1.0f)
00292 m_invregions.append(qMakePair(start, 1.0f));
00293 }
00294
00295 void MythUIEditBar::ClearImages(void)
00296 {
00297 while (m_images.size())
00298 DeleteChild(m_images.takeFirst());
00299 }
00300
00304 void MythUIEditBar::CopyFrom(MythUIType *base)
00305 {
00306 MythUIEditBar *editbar = dynamic_cast<MythUIEditBar *>(base);
00307
00308 if (!editbar)
00309 return;
00310
00311 m_position = editbar->m_position;
00312
00313 QListIterator<QPair<float, float> > it(m_regions);
00314
00315 while (it.hasNext())
00316 editbar->m_regions.append(it.next());
00317
00318 MythUIType::CopyFrom(base);
00319 }
00320
00324 void MythUIEditBar::CreateCopy(MythUIType *parent)
00325 {
00326 MythUIEditBar *editbar = new MythUIEditBar(parent, objectName());
00327 editbar->CopyFrom(this);
00328 }
00329
00333 void MythUIEditBar::Finalize(void)
00334 {
00335 MythUIType *position = GetChild("position");
00336
00337 if (position)
00338 position->MoveToTop();
00339 }