Huggle  build:^490^dce1e5c
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
hugglequeue.cpp
1 //This program is free software: you can redistribute it and/or modify
2 //it under the terms of the GNU General Public License as published by
3 //the Free Software Foundation, either version 3 of the License, or
4 //(at your option) any later version.
5 
6 //This program is distributed in the hope that it will be useful,
7 //but WITHOUT ANY WARRANTY; without even the implied warranty of
8 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 //GNU General Public License for more details.
10 
11 #include "hugglequeue.hpp"
12 #include "ui_hugglequeue.h"
13 
14 using namespace Huggle;
15 
16 HuggleQueue::HuggleQueue(QWidget *parent) : QDockWidget(parent), ui(new Ui::HuggleQueue)
17 {
18  ui->setupUi(this);
19  CurrentFilter = HuggleQueueFilter::DefaultFilter;
20  this->layout = new QVBoxLayout(ui->scrollArea);
21  this->layout->setMargin(0);
22  this->layout->setSpacing(0);
23  this->xx = new QWidget();
24  this->frame = new QFrame();
25  ui->scrollArea->setWidget(this->xx);
26  xx->setLayout(this->layout);
27  this->layout->addWidget(this->frame);
28  ui->comboBox->addItem(this->CurrentFilter->QueueName);
29  ui->comboBox->setCurrentIndex(0);
30 }
31 
32 HuggleQueue::~HuggleQueue()
33 {
34  delete layout;
35  delete CurrentFilter;
36  delete ui;
37 }
38 
39 void HuggleQueue::AddItem(WikiEdit *page)
40 {
41  if (page == NULL)
42  {
43  throw new Exception("WikiEdit *page must not be NULL", "void HuggleQueue::AddItem(WikiEdit *page)");
44  }
45  // so we need to insert the item somehow
46  HuggleQueueItemLabel *label = new HuggleQueueItemLabel(this);
47  page->RegisterConsumer(HUGGLECONSUMER_QUEUE);
48  // we no longer to prevent this from being deleted because we already have different lock for that
49  page->UnregisterConsumer("DeletionLock");
50  label->page = page;
51  label->SetName(page->Page->PageName);
52  if (page->Score <= MINIMAL_SCORE)
53  {
54  this->layout->addWidget(label);
55  } else
56  {
57  int id = 0;
59  {
60  while (GetScore(id) > page->Score && GetScore(id) > MINIMAL_SCORE)
61  {
62  id++;
63  }
64  }
65  else
66  {
67  while (GetScore(id) >= page->Score && GetScore(id) > MINIMAL_SCORE)
68  {
69  id++;
70  }
71  }
72  if (id >= this->layout->count() && this->layout->count() > 0)
73  {
74  id = this->layout->count() - 1;
75  }
76  this->layout->insertWidget(id, label);
77  }
78  this->Items.append(label);
79  HuggleQueueItemLabel::Count++;
80 }
81 
82 void HuggleQueue::Next()
83 {
84  if (HuggleQueueItemLabel::Count < 1)
85  {
86  return;
87  }
88  QLayoutItem *i = this->layout->itemAt(0);
89  HuggleQueueItemLabel *label = (HuggleQueueItemLabel*)i->widget();
90  label->Process(i);
91  this->layout->removeItem(i);
92  delete label;
93 }
94 
95 void HuggleQueue::DeleteByRevID(int RevID)
96 {
97  int c = 0;
98  while (c < this->Items.count())
99  {
100  HuggleQueueItemLabel *item = this->Items.at(c);
101  if (item->page->RevID == RevID)
102  {
103  if (Core::Main->CurrentEdit == item->page)
104  {
105  // we can't delete item that is being reviewed now
106  return;
107  }
108  this->Delete(item);
109  delete item;
110  return;
111  }
112  c++;
113  }
114 }
115 
116 void HuggleQueue::Delete(HuggleQueueItemLabel *item, QLayoutItem *qi)
117 {
118  if (item == NULL)
119  {
120  throw new Exception("HuggleQueueItemLabel *item must not be NULL in this context", "void HuggleQueue::Delete(HuggleQueueItemLabel *item, QLayoutItem *qi)");
121  }
122  if (qi != NULL)
123  {
124  item->page->UnregisterConsumer(HUGGLECONSUMER_QUEUE);
125  item->page = NULL;
126  this->layout->removeItem(qi);
127  return;
128  }
129  int curr=0;
130  while(curr<(this->layout->count()-1))
131  {
132  QLayoutItem *i = this->layout->itemAt(curr);
133  HuggleQueueItemLabel *label = (HuggleQueueItemLabel*)i->widget();
134  if (label == item)
135  {
136  this->layout->removeItem(i);
137  if (label->page != NULL)
138  {
139  label->page->UnregisterConsumer(HUGGLECONSUMER_QUEUE);
140  label->page = NULL;
141  }
142  break;
143  }
144  curr++;
145  }
146 }
147 
148 void HuggleQueue::Trim(int i)
149 {
150  if (i < 1)
151  {
152  throw new Huggle::Exception("Parameter i must be greater than 0 in HuggleQueue::Trim(i)");
153  }
154 
155  while (i > 0)
156  {
157  Trim();
158  i--;
159  }
160 }
161 
163 {
164  if (HuggleQueueItemLabel::Count < 1)
165  {
166  return;
167  }
168  int x = this->layout->count() - 1;
169  QLayoutItem *i = this->layout->itemAt(x);
170  if (i->widget() == this->frame)
171  {
172  x--;
173  i = this->layout->itemAt(x);
174  }
175  HuggleQueueItemLabel *label = (HuggleQueueItemLabel*)i->widget();
176  label->Remove();
177  this->layout->removeItem(i);
178  delete label;
179 }
180 
181 long HuggleQueue::GetScore(int id)
182 {
183  if (this->layout->count() - 1 <= id)
184  {
185  return MINIMAL_SCORE;
186  }
187 
188  QLayoutItem *i = this->layout->itemAt(id);
189  HuggleQueueItemLabel *label = (HuggleQueueItemLabel*)i->widget();
190  if (label->page == NULL)
191  {
192  return MINIMAL_SCORE;
193  }
194  return label->page->Score;
195 }
static bool QueueNewEditsUp
Whether new edits go to top or bottom.
int RevID
Revision ID.
Definition: wikiedit.hpp:105
void UnregisterConsumer(const int consumer)
This function will remove a string which prevent the object from being removed.
Definition: collectable.cpp:68
void RegisterConsumer(const int consumer)
Registers a consumer.
Definition: collectable.cpp:57
Queue of edits.
Definition: hugglequeue.hpp:36
Every exception raised by huggle is defined by this class.
Definition: exception.hpp:20
void Trim()
Remove 1 item.
This is item of queue, it is derived from qt object.
Wiki edit.
Definition: wikiedit.hpp:67
static MainWindow * Main
Pointer to main.
Definition: core.hpp:111
QString PageName
Name of page.
Definition: wikipage.hpp:48
WikiPage * Page
Page that was changed by edit.
Definition: wikiedit.hpp:87