Welcome to mirror list, hosted at ThFree Co, Russian Federation.

classificator_tree.cpp « qt - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3936fa78facfe4e61079fa864bdef03f910339dd (plain)
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include "../base/SRC_FIRST.hpp"
#include "classificator_tree.hpp"

#include "../indexer/classificator_loader.hpp"
#include "../indexer/classificator.hpp"

#include "../platform/platform.hpp"

#include "../base/assert.hpp"

#include "../std/bind.hpp"

#include <QtGui/QTreeWidget>
#include <QtGui/QHBoxLayout>
#include <QtGui/QCheckBox>
#include <QtGui/QPushButton>
#include <QtGui/QToolBar>
#include <QtGui/QFileDialog>

#include "../base/start_mem_debug.hpp"


namespace qt 
{

typedef ClassifObject::visible_mask_t mask_t;

///////////////////////////////////////////////////////////////////////////
// QClassifTree implementation
///////////////////////////////////////////////////////////////////////////

class QClassifTree : public QTreeWidget
{
  typedef QTreeWidget base_type;

  ClassifTreeHolder * get_parent()
  {
    return dynamic_cast<ClassifTreeHolder *>(parentWidget());
  }

public:
  QClassifTree(ClassifTreeHolder * pParent) : base_type(pParent) {}

protected:
  virtual void contextMenuEvent(QContextMenuEvent * /*e*/)
  {
    int const col = currentColumn();
    if (col == 1)
      get_parent()->EditItem(currentItem());
  }
};

///////////////////////////////////////////////////////////////////////////
// QEditChecks implementation
///////////////////////////////////////////////////////////////////////////

QEditChecks::QEditChecks(QWidget * pParent)
: base_type(pParent)
{
  QVBoxLayout * pLayout = new QVBoxLayout(this);

  for (size_t i = 0; i < s_count; ++i)
  {
    m_arr[i] = new QCheckBox(QString::number(i), this);
    pLayout->addWidget(m_arr[i]);
  }

  QPushButton * p = new QPushButton(tr("OK"), this);
  connect(p, SIGNAL(pressed()), this, SLOT(OnOK()));
  pLayout->addWidget(p);

  setLayout(pLayout);
}

void QEditChecks::Show(ClassifObject * p, QPoint const & pt)
{
  m_pObj = p;
  mask_t mask = p->GetVisibilityMask();

  for (size_t i = 0; i < s_count; ++i)
    m_arr[i]->setChecked(mask[i]);

  show();
  move(pt);
}

void QEditChecks::OnOK()
{
  mask_t mask;
  for (size_t i = 0; i < s_count; ++i)
    mask[i] = m_arr[i]->isChecked();

  close();

  m_pObj->SetVisibilityMask(mask);
  emit applied();
}

///////////////////////////////////////////////////////////////////////////
// QClassifTreeHolder implementation
///////////////////////////////////////////////////////////////////////////

ClassifTreeHolder::ClassifTreeHolder(QWidget * pParent,
                                     QWidget * drawWidget, char const * drawSlot)
: base_type(pParent)
{
  QVBoxLayout * pLayout = new QVBoxLayout(this);
  pLayout->setContentsMargins(0, 0, 0, 0);

  QToolBar * pToolBar = new QToolBar(this);
  pToolBar->setIconSize(QSize(32, 32));
  pToolBar->addAction(QIcon(":/classif32/save.png"), tr("Save visibility settings"), this, SLOT(OnSave()));
  pToolBar->addAction(QIcon(":/classif32/load.png"), tr("Load visibility settings"), this, SLOT(OnLoad()));
  pToolBar->addAction(QIcon(":/classif32/select.png"), tr("Select all checks"), this, SLOT(OnSelectAll()));
  pToolBar->addAction(QIcon(":/classif32/clear.png"), tr("Clear all checks"), this, SLOT(OnClearAll()));

  m_pTree = new QClassifTree(this);
  m_pEditor = new QEditChecks(this);

  connect(m_pEditor, SIGNAL(applied()), this, SLOT(OnEditFinished()));
  drawWidget->connect(m_pEditor, SIGNAL(applied()), drawWidget, drawSlot);
  drawWidget->connect(this, SIGNAL(redraw_model()), drawWidget, drawSlot);

  m_pTree->setColumnCount(2);

  QStringList headers;
  headers << tr("Type") << tr("Mask");
  m_pTree->setHeaderLabels(headers);

  pLayout->addWidget(pToolBar);
  pLayout->addWidget(m_pTree);
  setLayout(pLayout);
}

namespace 
{
  void to_item(QTreeWidgetItem * p, ClassifObject * pObj)
  {
    qulonglong ptr = reinterpret_cast<qulonglong>(pObj);
    return p->setData(1, Qt::UserRole, QVariant(ptr));
  }

  ClassifObject * from_item(QTreeWidgetItem * p)
  {
    bool isOK;
    qulonglong ptr = p->data(1, Qt::UserRole).toULongLong(&isOK);
    ASSERT ( isOK, () );
    return reinterpret_cast<ClassifObject *>(ptr);
  }
}

void ClassifTreeHolder::Process(QTreeWidgetItem * pParent, ClassifObject * p)
{
  QTreeWidgetItem * pItem = 0;

  // do not add root item (leave more useful space)
  if (p != m_pRoot)
  {
    QStringList values;
    values << QString::fromStdString(p->GetName()) << GetMaskValue(p);

    if (pParent)
      pItem = new QTreeWidgetItem(pParent, values);
    else
      pItem = new QTreeWidgetItem(m_pTree, values);

    to_item(pItem, p);
  }

  p->ForEachObject(bind(&ClassifTreeHolder::Process, this, pItem, _1));
}

QString ClassifTreeHolder::GetMaskValue(ClassifObject const * p) const
{
  mask_t mask = p->GetVisibilityMask();
  size_t const count = mask.size();
  string str;
  str.resize(count);
  for (size_t i = 0; i < mask.size(); ++i)
    str[i] = (mask[i] ? '1' : '0');

  return QString::fromStdString(str);
}

void ClassifTreeHolder::SetRoot(ClassifObject * pRoot)
{
  m_pTree->clear();

  m_pRoot = pRoot;
  Process(0, m_pRoot);
  m_pTree->expandAll();
}

void ClassifTreeHolder::EditItem(QTreeWidgetItem * p)
{
  m_pCurrent = p;
  ClassifObject * pObj = from_item(p);

  // find best position of edit-window newar the cursor
  QPoint pt = QCursor::pos();
  int const h = m_pEditor->frameSize().height();
  pt.ry() -= (h / 2);

  pt.ry() = max(0, pt.y());
  pt.ry() = min(mapToGlobal(rect().bottomRight()).y() - h, pt.y());

  // show window
  m_pEditor->Show(pObj, pt);
}

void ClassifTreeHolder::OnEditFinished()
{
  m_pCurrent->setText(1, GetMaskValue(from_item(m_pCurrent)));
}

void ClassifTreeHolder::OnSave()
{
  QString const fName = QFileDialog::getSaveFileName(this,
    tr("Save classificator visibility"),
    QString::fromStdString(GetPlatform().WritableDir()),
    tr("Text Files (*.txt)"));

  classif().PrintVisibility(fName.toAscii().constData());
}

void ClassifTreeHolder::OnLoad()
{
  QString const fName = QFileDialog::getOpenFileName(this,
    tr("Open classificator visibility"),
    QString::fromStdString(GetPlatform().WritableDir()),
    tr("Text Files (*.txt)"));

  classificator::ReadVisibility(fName.toAscii().constData());

  Rebuild();
}

namespace
{
  template <class ToDo> void ForEachRecursive(ClassifObject * p, ToDo & toDo)
  {
    toDo(p);
    p->ForEachObject(bind(&ForEachRecursive<ToDo>, _1, ref(toDo)));
  }

  class do_select
  {
    mask_t m_mask;
  public:
    do_select(mask_t mask) : m_mask(mask) {}
    void operator() (ClassifObject * p)
    {
      p->SetVisibilityMask(m_mask);
    }
  };
}

template <class TMask>
void ClassifTreeHolder::OnSetMask(TMask mask)
{
  do_select doSelect(mask);
  ForEachRecursive(GetRoot(), doSelect);
  Rebuild();
}

void ClassifTreeHolder::OnSelectAll()
{
  mask_t mask;
  mask.set();
  OnSetMask(mask);
}

void ClassifTreeHolder::OnClearAll()
{
  mask_t mask;
  mask.reset();
  OnSetMask(mask);
}

ClassifObject * ClassifTreeHolder::GetRoot()
{
  return classif().GetMutableRoot();
}

void ClassifTreeHolder::Rebuild()
{
  SetRoot(GetRoot());
  Redraw();
}

void ClassifTreeHolder::Redraw()
{
  emit redraw_model();
}

}