File: QT4SelectItems.cc

package info (click to toggle)
rdkit 202209.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 203,880 kB
  • sloc: cpp: 334,239; python: 80,247; ansic: 24,579; java: 7,667; sql: 2,123; yacc: 1,884; javascript: 1,358; lex: 1,260; makefile: 576; xml: 229; fortran: 183; cs: 181; sh: 101
file content (99 lines) | stat: -rw-r--r-- 3,074 bytes parent folder | download | duplicates (3)
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
//
// file QT4SelectItems.H
// Dave Cosgrove
// AstraZeneca
// 27th June 2006
//
// Puts up a QListWidget in a box allowing selections from a list of strings.
// Similar to QTSelectItems.H but in Qt4 speak.
// This must always be used as a modal dialog, since the address of the vector
// holding the selections is kept, and this must be in scope when it is filled.
// It is filled when either the ok or cancel buttons are pressed.

#include <QLabel>
#include <QLayout>
#include <QListWidget>
#include <QListWidgetItem>
#include <QPushButton>

#include "QT4SelectItems.H"

#include <iostream>

using namespace std;

namespace DACLIB {

// ***************************************************************************
QT4SelectItems::QT4SelectItems(const string &label,
                               vector<QString> &item_labels,
                               vector<char> &selected_items, bool radio_box,
                               QWidget *parent)
    : QDialog(parent) {
  setWindowTitle("Select Items");

  QWidget *vbox = new QWidget(this);

  list_widget_ = new QListWidget();

  if (radio_box) {
    list_widget_->setSelectionMode(QAbstractItemView::SingleSelection);
  } else {
    list_widget_->setSelectionMode(QAbstractItemView::ExtendedSelection);
  }

  vector<QString>::iterator p, ps;
  int i = 0;
  for (p = item_labels.begin(), ps = item_labels.end(); p != ps; ++p, ++i) {
    QListWidgetItem *item = new QListWidgetItem(*p, list_widget_);
    list_widget_->setItemSelected(item, selected_items[i]);
  }

  connect(list_widget_, SIGNAL(itemDoubleClicked(QListWidgetItem *)), this,
          SLOT(slot_list_double_clicked(QListWidgetItem *)));

  build_action_box();

  vlayout_ = new QVBoxLayout();
  vlayout_->setDirection(QBoxLayout::BottomToTop);
  vlayout_->addWidget(action_box_);
  vlayout_->addWidget(list_widget_);
  vlayout_->addWidget(new QLabel(label.c_str()));

  vbox->setLayout(vlayout_);
  vbox->show();
}

// *****************************************************************************
void QT4SelectItems::get_results(vector<char> &selected_items) const {
  selected_items = vector<char>(list_widget_->count(), 0);
  for (int i = 0, is = list_widget_->count(); i < is; ++i) {
    selected_items[i] = list_widget_->isItemSelected(list_widget_->item(i));
  }
}

// *****************************************************************************
void QT4SelectItems::build_action_box() {
  action_box_ = new QWidget(this);
  QHBoxLayout *hlayout = new QHBoxLayout();

  QPushButton *button = new QPushButton("Ok");
  button->setDefault(true);
  hlayout->addWidget(button);
  connect(button, SIGNAL(clicked()), this, SLOT(accept()));

  button = new QPushButton("Cancel");
  hlayout->addWidget(button);
  connect(button, SIGNAL(clicked()), this, SLOT(reject()));

  action_box_->setLayout(hlayout);
}

// *****************************************************************************
// select item and out
void QT4SelectItems::slot_list_double_clicked(QListWidgetItem *item) {
  item->setSelected(true);
  accept();
}

}  // namespace DACLIB