@@ -1,230 +1,230 | |||
|
1 | 1 | /*! |
|
2 | 2 | \class Widget |
|
3 | 3 | \brief Ui for the application. |
|
4 | 4 | \internal |
|
5 | 5 | */ |
|
6 | 6 | |
|
7 | 7 | #include "widget.h" |
|
8 | 8 | #include <QGridLayout> |
|
9 | 9 | #include <QPushButton> |
|
10 | 10 | #include <QLabel> |
|
11 | 11 | |
|
12 | 12 | #include <QSqlQuery> |
|
13 | 13 | #include <qscatterseries.h> |
|
14 | 14 | #include <qchartview.h> |
|
15 | 15 | #include <qchartaxis.h> |
|
16 | 16 | #include <qbarset.h> |
|
17 | 17 | #include <QListWidget> |
|
18 | 18 | #include <QPrinter> |
|
19 | 19 | #include <QPrintDialog> |
|
20 | 20 | #include <QRadioButton> |
|
21 | 21 | #include <QStringList> |
|
22 | 22 | #include <qbarseries.h> |
|
23 | 23 | |
|
24 | 24 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
25 | 25 | |
|
26 | 26 | Widget::Widget(QWidget *parent) |
|
27 | 27 | : QWidget(parent) |
|
28 | 28 | { |
|
29 | 29 | setGeometry(100, 100, 1000, 600); |
|
30 | 30 | |
|
31 | 31 | // right panel layout |
|
32 | 32 | barChartRadioButton = new QRadioButton(tr("Bar chart")); |
|
33 | 33 | barChartRadioButton->setChecked(true); |
|
34 | 34 | scatterChartRadioButton = new QRadioButton(tr("Scatter chart")); |
|
35 | 35 | scatterChartRadioButton->setChecked(false); |
|
36 | 36 | countrieslist = new QListWidget; |
|
37 | 37 | countrieslist->setSelectionMode(QAbstractItemView::MultiSelection); |
|
38 | 38 | |
|
39 | 39 | //list of years widget |
|
40 | 40 | yearslist = new QListWidget; |
|
41 | 41 | yearslist->setSelectionMode(QAbstractItemView::ExtendedSelection); |
|
42 | 42 | for (int i = 1990; i < 2011; i++) |
|
43 | 43 | yearslist->addItem(QString("%1").arg(i)); |
|
44 | 44 | |
|
45 | 45 | QPushButton* refreshButton = new QPushButton(tr("Refresh")); |
|
46 | 46 | connect(refreshButton, SIGNAL(clicked()), this, SLOT(refreshChart())); |
|
47 | 47 | |
|
48 | 48 | QPushButton* printButton = new QPushButton(tr("Print chart")); |
|
49 | 49 | connect(printButton, SIGNAL(clicked()), this, SLOT(printChart())); |
|
50 | 50 | |
|
51 | 51 | QVBoxLayout* rightPanelLayout = new QVBoxLayout; |
|
52 | 52 | rightPanelLayout->addWidget(barChartRadioButton); |
|
53 | 53 | rightPanelLayout->addWidget(scatterChartRadioButton); |
|
54 | 54 | rightPanelLayout->addWidget(countrieslist); |
|
55 | 55 | rightPanelLayout->addWidget(yearslist); |
|
56 | 56 | rightPanelLayout->addWidget(refreshButton); |
|
57 | 57 | rightPanelLayout->addWidget(printButton); |
|
58 | 58 | rightPanelLayout->setStretch(0, 1); |
|
59 | 59 | rightPanelLayout->setStretch(1, 0); |
|
60 | 60 | |
|
61 | 61 | // main layout |
|
62 | 62 | chartArea = new QChartView(this); |
|
63 | 63 | chartArea->setChartTitle("GDP by country"); |
|
64 | 64 | QGridLayout* mainLayout = new QGridLayout; |
|
65 | 65 | mainLayout->addWidget(chartArea, 0, 0); |
|
66 | 66 | mainLayout->addLayout(rightPanelLayout, 0, 1); |
|
67 | 67 | mainLayout->setColumnStretch(0,1); |
|
68 | 68 | setLayout(mainLayout); |
|
69 | 69 | |
|
70 | 70 | // connect to the database |
|
71 | 71 | db = QSqlDatabase::addDatabase("QSQLITE"); |
|
72 | 72 | db.setDatabaseName("gdpData"); |
|
73 | 73 | if(!db.open()) |
|
74 | 74 | { |
|
75 | 75 | qDebug() << "could not open database. SQLite db file missing (?)"; |
|
76 | 76 | return; |
|
77 | 77 | } |
|
78 | 78 | |
|
79 | 79 | // get the list of all countires and regions. |
|
80 | 80 | QSqlQuery query; |
|
81 | 81 | query.exec("SELECT DISTINCT country FROM gdp2"); |
|
82 | 82 | |
|
83 | 83 | // add the countries to the country filter |
|
84 | 84 | while (query.next()) { |
|
85 | 85 | countrieslist->addItem(query.value(0).toString()); |
|
86 | 86 | } |
|
87 | 87 | |
|
88 | 88 | // hide axis X labels |
|
89 | QChartAxis* axis = chartArea->axisX(); | |
|
89 | //QChartAxis* axis = chartArea->axisX(); | |
|
90 | 90 | // axis-> |
|
91 | 91 | // axis->setLabelsVisible(false); |
|
92 | 92 | // newAxis.setLabelsOrientation(QChartAxis::LabelsOrientationSlide); |
|
93 | 93 | |
|
94 | 94 | } |
|
95 | 95 | |
|
96 | 96 | Widget::~Widget() |
|
97 | 97 | { |
|
98 | 98 | // |
|
99 | 99 | db.close(); |
|
100 | 100 | } |
|
101 | 101 | |
|
102 | 102 | /*! |
|
103 | 103 | refreshes the chart |
|
104 | 104 | */ |
|
105 | 105 | void Widget::refreshChart() |
|
106 | 106 | { |
|
107 | 107 | chartArea->removeAllSeries(); |
|
108 | 108 | |
|
109 | 109 | // selected countries items list is not sorted. copy the values to QStringlist and sort them. |
|
110 | 110 | QStringList selectedCountriesStrings; |
|
111 | 111 | QList<QListWidgetItem*> selectedCountriesItems = countrieslist->selectedItems(); |
|
112 | 112 | for (int i = 0; i < selectedCountriesItems.size(); i++) |
|
113 | 113 | selectedCountriesStrings.append(selectedCountriesItems[i]->text()); |
|
114 | 114 | selectedCountriesStrings.sort(); |
|
115 | 115 | |
|
116 | 116 | QSqlQuery query; |
|
117 | 117 | // selected years items list is not sorted. copy the values to QList<int> and sort them. |
|
118 | 118 | QList<int> selectedYearsInts; |
|
119 | 119 | QList<QListWidgetItem*> selectedYearsItems = yearslist->selectedItems(); |
|
120 | 120 | for (int i = 0; i < selectedYearsItems.size(); i++) |
|
121 | 121 | selectedYearsInts.append(selectedYearsItems[i]->text().toInt()); |
|
122 | 122 | qSort(selectedYearsInts.begin(), selectedYearsInts.end(), qGreater<int>()); |
|
123 | 123 | |
|
124 | 124 | if (barChartRadioButton->isChecked()) |
|
125 | 125 | { |
|
126 | 126 | // use the sorted selected coutries list to initialize BarCategory |
|
127 | 127 | QStringList category; |
|
128 | 128 | for (int i = 0; i < selectedCountriesStrings.size(); i++) |
|
129 | 129 | category << selectedCountriesStrings[i]; |
|
130 | 130 | QBarSeries* series0 = new QBarSeries(category); |
|
131 | 131 | series0 = new QBarSeries(category); |
|
132 | 132 | |
|
133 | 133 | // prepare the selected counries SQL query |
|
134 | 134 | QString countriesQuery = "country IN ("; |
|
135 | 135 | for (int i = 0; i < selectedCountriesStrings.size(); i++) |
|
136 | 136 | { |
|
137 | 137 | countriesQuery.append("'" + selectedCountriesStrings[i] + "'"); |
|
138 | 138 | if ( i < selectedCountriesStrings.size() - 1) |
|
139 | 139 | countriesQuery.append(","); |
|
140 | 140 | else |
|
141 | 141 | countriesQuery.append(")"); |
|
142 | 142 | } |
|
143 | 143 | |
|
144 | 144 | // perform a query for each selected year |
|
145 | 145 | for (int i = 0; i < selectedYearsInts.size(); i++) |
|
146 | 146 | { |
|
147 | 147 | query.exec("SELECT country,gdpvalue FROM gdp2 where year=" + QString("%1").arg(selectedYearsInts[i]) + " AND " + countriesQuery); |
|
148 | 148 | QBarSet* barSet = new QBarSet("Barset" + QString::number(i)); |
|
149 | 149 | |
|
150 | 150 | // while (query.next()) { |
|
151 | 151 | // qDebug() << query.value(0).toString() << " : " << query.value(1).toString(); |
|
152 | 152 | // } |
|
153 | 153 | query.first(); |
|
154 | 154 | |
|
155 | 155 | // the data for some of the coutries for some years might be missing. |
|
156 | 156 | // QBarChart needs bars to have same size |
|
157 | 157 | for (int k = 0; k < selectedCountriesStrings.size(); k++) |
|
158 | 158 | { |
|
159 | 159 | if (selectedCountriesStrings[k] == query.value(0).toString()) |
|
160 | 160 | { |
|
161 | 161 | *barSet << query.value(1).toReal(); |
|
162 | 162 | qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[i]); |
|
163 | 163 | query.next(); |
|
164 | 164 | } |
|
165 | 165 | else |
|
166 | 166 | { |
|
167 | 167 | // data missing, put 0 |
|
168 | 168 | *barSet << 0.0f; |
|
169 | 169 | qDebug() << "Putting 0 for the missing data" << " : " << QString("%1").arg(selectedYearsInts[i]); |
|
170 | 170 | } |
|
171 | 171 | } |
|
172 | 172 | series0->addBarSet(barSet); |
|
173 | 173 | } |
|
174 | 174 | // add the serie to the chart |
|
175 | 175 | chartArea->addSeries(series0); |
|
176 | 176 | } |
|
177 | 177 | else if (scatterChartRadioButton->isChecked()) |
|
178 | 178 | { |
|
179 | 179 | QString yearsQuery = "year IN ("; |
|
180 | 180 | for (int i = 0; i < selectedYearsInts.size(); i++) |
|
181 | 181 | { |
|
182 | 182 | yearsQuery.append("'" + QString("%1").arg(selectedYearsInts[i]) + "'"); |
|
183 | 183 | if ( i < selectedYearsInts.size() - 1) |
|
184 | 184 | yearsQuery.append(","); |
|
185 | 185 | else |
|
186 | 186 | yearsQuery.append(")"); |
|
187 | 187 | } |
|
188 | 188 | |
|
189 | 189 | // perform a query for each selected country |
|
190 | 190 | for (int i = 0; i < selectedCountriesStrings.size(); i++) |
|
191 | 191 | { |
|
192 | 192 | query.exec("SELECT year,gdpvalue FROM gdp2 where country='" + selectedCountriesStrings[i] + "' AND " + yearsQuery); |
|
193 | 193 | query.first(); |
|
194 | 194 | |
|
195 | 195 | QScatterSeries* series = new QScatterSeries; |
|
196 | 196 | // the data for some of the coutries for some years might be missing. |
|
197 | 197 | for (int k = 0; k < selectedYearsInts.size(); k++) |
|
198 | 198 | { |
|
199 | 199 | if (selectedYearsInts[k] == query.value(0).toInt()) |
|
200 | 200 | { |
|
201 | 201 | *series << QPointF(query.value(0).toInt() , query.value(1).toReal()); |
|
202 | 202 | qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[k]); |
|
203 | 203 | query.next(); |
|
204 | 204 | } |
|
205 | 205 | else |
|
206 | 206 | { |
|
207 | 207 | // data missing, put 0 |
|
208 | 208 | *series << QPointF(selectedYearsInts[k] , 0.0f); |
|
209 | 209 | qDebug() << "Putting 0 for the missing data" << " : " << QString("%1").arg(selectedYearsInts[i]) << " " << query.value(0).toInt(); |
|
210 | 210 | } |
|
211 | 211 | } |
|
212 | 212 | // chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1); |
|
213 | 213 | chartArea->addSeries(series); |
|
214 | 214 | } |
|
215 | 215 | chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] - 1, selectedYearsInts[0] + 1); |
|
216 | 216 | } |
|
217 | 217 | } |
|
218 | 218 | |
|
219 | 219 | void Widget::printChart() |
|
220 | 220 | { |
|
221 | 221 | QPrinter printer; |
|
222 | 222 | // QPrinter printer(QPrinter::HighResolution); |
|
223 | 223 | printer.setOutputFormat(QPrinter::PdfFormat); |
|
224 | 224 | printer.setOrientation(QPrinter::Landscape); |
|
225 | 225 | printer.setOutputFileName("print.pdf"); |
|
226 | 226 | |
|
227 | 227 | QPainter painter; |
|
228 | 228 | painter.begin(&printer); |
|
229 | 229 | chartArea->render(&painter); |
|
230 | 230 | } |
@@ -1,149 +1,150 | |||
|
1 | 1 | #include <QtGui/QApplication> |
|
2 | 2 | #include <QMainWindow> |
|
3 | 3 | #include <qchartglobal.h> |
|
4 | 4 | #include <qchartview.h> |
|
5 | 5 | #include <qstackedbarseries.h> |
|
6 | 6 | #include <qbarset.h> |
|
7 | 7 | #include <qchartaxis.h> |
|
8 | 8 | #include <qlegend.h> |
|
9 | 9 | #include <QStringList> |
|
10 | 10 | #include <QDebug> |
|
11 | 11 | |
|
12 | 12 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
13 | 13 | |
|
14 | 14 | //! [1] |
|
15 | 15 | class DrilldownBarSeries : public QStackedBarSeries |
|
16 | 16 | { |
|
17 | 17 | Q_OBJECT |
|
18 | 18 | public: |
|
19 | 19 | DrilldownBarSeries(QStringList categories, QObject *parent = 0) : QStackedBarSeries(categories,parent) {} |
|
20 | 20 | |
|
21 | 21 | void mapDrilldownSeries(QString category, DrilldownBarSeries* drilldownSeries) |
|
22 | 22 | { |
|
23 | 23 | mDrilldownSeries[category] = drilldownSeries; |
|
24 | 24 | } |
|
25 | 25 | |
|
26 | 26 | DrilldownBarSeries* drilldownSeries(QString category) |
|
27 | 27 | { |
|
28 | 28 | return mDrilldownSeries[category]; |
|
29 | 29 | } |
|
30 | 30 | |
|
31 | 31 | public Q_SLOTS: |
|
32 | 32 | |
|
33 | 33 | private: |
|
34 | 34 | |
|
35 | 35 | QMap<QString, DrilldownBarSeries*> mDrilldownSeries; |
|
36 | 36 | }; |
|
37 | 37 | //! [1] |
|
38 | 38 | |
|
39 | 39 | //! [2] |
|
40 | 40 | class DrilldownChart : public QChartView |
|
41 | 41 | { |
|
42 | 42 | Q_OBJECT |
|
43 | 43 | public: |
|
44 | 44 | explicit DrilldownChart(QWidget *parent = 0) : QChartView(parent), m_currentSeries(0) {} |
|
45 | 45 | |
|
46 | 46 | void changeSeries(QSeries* series) |
|
47 | 47 | { |
|
48 | 48 | if (m_currentSeries) |
|
49 | 49 | removeSeries(m_currentSeries); |
|
50 | 50 | m_currentSeries = series; |
|
51 | 51 | addSeries(series); |
|
52 | 52 | setChartTitle(series->title()); |
|
53 | 53 | } |
|
54 | 54 | |
|
55 | 55 | public Q_SLOTS: |
|
56 | 56 | void handleRightClick(QBarSet *barset, QString category) |
|
57 | 57 | { |
|
58 | Q_UNUSED(barset) | |
|
58 | 59 | DrilldownBarSeries* series = static_cast<DrilldownBarSeries*> (sender()); |
|
59 | 60 | changeSeries(series->drilldownSeries(category)); |
|
60 | 61 | } |
|
61 | 62 | |
|
62 | 63 | private: |
|
63 | 64 | QSeries* m_currentSeries; |
|
64 | 65 | }; |
|
65 | 66 | //! [2] |
|
66 | 67 | |
|
67 | 68 | int main(int argc, char *argv[]) |
|
68 | 69 | { |
|
69 | 70 | QApplication a(argc, argv); |
|
70 | 71 | QMainWindow window; |
|
71 | 72 | |
|
72 | 73 | DrilldownChart* drilldownChart = new DrilldownChart(&window); |
|
73 | 74 | drilldownChart->setChartTheme(QChart::ChartThemeIcy); |
|
74 | 75 | |
|
75 | 76 | //! [3] |
|
76 | 77 | // Define categories |
|
77 | 78 | QStringList months; |
|
78 | 79 | months << "May" << "Jun" << "Jul" << "Aug" << "Sep"; |
|
79 | 80 | QStringList weeks; |
|
80 | 81 | weeks << "week 1" << "week 2" << "week 3" << "week 4"; |
|
81 | 82 | QStringList plants; |
|
82 | 83 | plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo"; |
|
83 | 84 | //! [3] |
|
84 | 85 | |
|
85 | 86 | //! [4] |
|
86 | 87 | // Create drilldown structure |
|
87 | 88 | DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart); |
|
88 | 89 | seasonSeries->setTitle("Crop by month - Season"); |
|
89 | 90 | |
|
90 | 91 | // Each month in season series has drilldown series for weekly data |
|
91 | 92 | foreach (QString month, months) { |
|
92 | 93 | |
|
93 | 94 | // Create drilldown series for every week |
|
94 | 95 | DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart); |
|
95 | 96 | seasonSeries->mapDrilldownSeries(month, weeklySeries); |
|
96 | 97 | |
|
97 | 98 | // Drilling down from weekly data brings us back to season data. |
|
98 | 99 | foreach (QString week, weeks) { |
|
99 | 100 | weeklySeries->mapDrilldownSeries(week, seasonSeries); |
|
100 | 101 | weeklySeries->setTitle(QString("Crop by week - " + month)); |
|
101 | 102 | } |
|
102 | 103 | |
|
103 | 104 | // Use right click signal to implement drilldown |
|
104 | 105 | QObject::connect(weeklySeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString))); |
|
105 | 106 | } |
|
106 | 107 | |
|
107 | 108 | // Enable drilldown from season series using right click. |
|
108 | 109 | QObject::connect(seasonSeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString))); |
|
109 | 110 | //! [4] |
|
110 | 111 | |
|
111 | 112 | //! [5] |
|
112 | 113 | // Fill monthly and weekly series with data |
|
113 | 114 | foreach (QString plant, plants) { |
|
114 | 115 | QBarSet* monthlyCrop = new QBarSet(plant); |
|
115 | 116 | foreach (QString month, months) { |
|
116 | 117 | QBarSet* weeklyCrop = new QBarSet(plant); |
|
117 | 118 | foreach (QString week, weeks ) { |
|
118 | 119 | *weeklyCrop << (qrand() % 20); |
|
119 | 120 | } |
|
120 | 121 | // Get the drilldown series from season series and add crop to it. |
|
121 | 122 | seasonSeries->drilldownSeries(month)->addBarSet(weeklyCrop); |
|
122 | 123 | seasonSeries->drilldownSeries(month)->setToolTipEnabled(true); |
|
123 | 124 | *monthlyCrop << weeklyCrop->total(); |
|
124 | 125 | |
|
125 | 126 | QObject::connect(weeklyCrop,SIGNAL(clicked(QString)),weeklyCrop,SIGNAL(toggleFloatingValues())); |
|
126 | 127 | } |
|
127 | 128 | seasonSeries->addBarSet(monthlyCrop); |
|
128 | 129 | QObject::connect(monthlyCrop,SIGNAL(clicked(QString)),monthlyCrop,SIGNAL(toggleFloatingValues())); |
|
129 | 130 | } |
|
130 | 131 | //! [5] |
|
131 | 132 | |
|
132 | 133 | seasonSeries->setToolTipEnabled(true); |
|
133 | 134 | |
|
134 | 135 | //! [6] |
|
135 | 136 | // Show season series in initial view |
|
136 | 137 | drilldownChart->changeSeries(seasonSeries); |
|
137 | 138 | drilldownChart->setChartTitle(seasonSeries->title()); |
|
138 | 139 | //! [6] |
|
139 | 140 | |
|
140 | 141 | drilldownChart->axisX()->setGridLineVisible(false); |
|
141 | 142 | |
|
142 | 143 | window.setCentralWidget(drilldownChart); |
|
143 | 144 | window.resize(400, 300); |
|
144 | 145 | window.show(); |
|
145 | 146 | |
|
146 | 147 | return a.exec(); |
|
147 | 148 | } |
|
148 | 149 | |
|
149 | 150 | #include "main.moc" |
@@ -1,146 +1,151 | |||
|
1 | 1 | #include "customtablemodel.h" |
|
2 | 2 | |
|
3 | 3 | CustomTableModel::CustomTableModel(QObject *parent) : |
|
4 | 4 | QAbstractTableModel(parent) |
|
5 | 5 | { |
|
6 | 6 | m_points.append(QPointF(10, 50)); |
|
7 | 7 | m_labels.append("Apples"); |
|
8 | 8 | m_points.append(QPointF(60, 70)); |
|
9 | 9 | m_labels.append("Oranges"); |
|
10 | 10 | m_points.append(QPointF(110, 50)); |
|
11 | 11 | m_labels.append("Bananas"); |
|
12 | 12 | m_points.append(QPointF(140, 40)); |
|
13 | 13 | m_labels.append("Lemons"); |
|
14 | 14 | m_points.append(QPointF(200, 150)); |
|
15 | 15 | m_labels.append("Plums"); |
|
16 | 16 | m_points.append(QPointF(225, 75)); |
|
17 | 17 | m_labels.append("Pearls"); |
|
18 | 18 | } |
|
19 | 19 | |
|
20 | 20 | int CustomTableModel::rowCount(const QModelIndex & parent) const |
|
21 | 21 | { |
|
22 | Q_UNUSED(parent) | |
|
22 | 23 | return m_points.count(); |
|
23 | 24 | } |
|
24 | 25 | |
|
25 | 26 | int CustomTableModel::columnCount(const QModelIndex & parent) const |
|
26 | 27 | { |
|
28 | Q_UNUSED(parent) | |
|
27 | 29 | return 3; |
|
28 | 30 | } |
|
29 | 31 | |
|
30 | 32 | QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const |
|
31 | 33 | { |
|
32 | 34 | if (role != Qt::DisplayRole) |
|
33 | 35 | return QVariant(); |
|
34 | 36 | |
|
35 | 37 | if (orientation == Qt::Horizontal) |
|
36 | 38 | { |
|
37 | 39 | switch(section) |
|
38 | 40 | { |
|
39 | 41 | case 0: |
|
40 | 42 | return "x"; |
|
41 | 43 | case 1: |
|
42 | 44 | return "y"; |
|
43 | 45 | case 2: |
|
44 | 46 | return "Fruit"; |
|
45 | 47 | default: |
|
46 | 48 | return "What?"; |
|
47 | 49 | } |
|
48 | 50 | } |
|
49 | 51 | else |
|
50 | 52 | return QString("%1").arg(section + 1); |
|
51 | 53 | } |
|
52 | 54 | |
|
53 | 55 | QVariant CustomTableModel::data(const QModelIndex & index, int role) const |
|
54 | 56 | { |
|
55 | 57 | if (role == Qt::DisplayRole) |
|
56 | 58 | { |
|
57 | 59 | switch(index.column()) |
|
58 | 60 | { |
|
59 | 61 | case 0: |
|
60 | 62 | return m_points[index.row()].x(); |
|
61 | 63 | case 1: |
|
62 | 64 | return m_points[index.row()].y(); |
|
63 | 65 | case 2: |
|
64 | 66 | return m_labels[index.row()]; |
|
65 | 67 | default: |
|
66 |
|
|
|
68 | break; | |
|
67 | 69 | } |
|
68 | 70 | } |
|
69 | 71 | else if (role == Qt::EditRole) |
|
70 | 72 | { |
|
71 | 73 | switch(index.column()) |
|
72 | 74 | { |
|
73 | 75 | case 0: |
|
74 | 76 | return m_points[index.row()].x(); |
|
75 | 77 | case 1: |
|
76 | 78 | return m_points[index.row()].y(); |
|
77 | 79 | case 2: |
|
78 | 80 | return m_labels[index.row()]; |
|
79 | 81 | default: |
|
80 |
|
|
|
82 | break; | |
|
81 | 83 | } |
|
82 | 84 | } |
|
85 | return QVariant(); | |
|
83 | 86 | } |
|
84 | 87 | |
|
85 | 88 | bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & value, int role) |
|
86 | 89 | { |
|
87 | 90 | if (index.isValid() && role == Qt::EditRole) |
|
88 | 91 | { |
|
89 | 92 | switch(index.column()) |
|
90 | 93 | { |
|
91 | 94 | case 0: |
|
92 | 95 | m_points[index.row()].setX(value.toDouble()); |
|
93 | 96 | break; |
|
94 | 97 | case 1: |
|
95 | 98 | m_points[index.row()].setY(value.toDouble()); |
|
96 | 99 | break; |
|
97 | 100 | case 2: |
|
98 | 101 | m_labels.replace(index.row(), value.toString()); |
|
99 | 102 | break; |
|
100 | 103 | default: |
|
101 | 104 | return false; |
|
102 | 105 | } |
|
103 | 106 | emit dataChanged(index, index); |
|
104 | 107 | return true; |
|
105 | 108 | } |
|
106 | 109 | return false; |
|
107 | 110 | } |
|
108 | 111 | |
|
109 | 112 | Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const |
|
110 | 113 | { |
|
111 | 114 | // if (!index.isValid()) |
|
112 | 115 | // return Qt::ItemIsEnabled; |
|
113 | 116 | return QAbstractItemModel::flags(index) | Qt::ItemIsEditable; |
|
114 | 117 | } |
|
115 | 118 | |
|
116 | 119 | bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & parent) |
|
117 | 120 | { |
|
121 | Q_UNUSED(parent) | |
|
122 | ||
|
118 | 123 | if (row < 0) |
|
119 | 124 | row = 0; |
|
120 | 125 | beginInsertRows(QModelIndex(), row /*dataTable.count()*/, row + count - 1); |
|
121 | 126 | for (int i = row; i < row + count; i++) |
|
122 | 127 | { |
|
123 | 128 | m_points.insert(row, QPointF(10,20)); |
|
124 | 129 | m_labels.insert(row,("a")); |
|
125 | 130 | } |
|
126 | 131 | endInsertRows(); |
|
127 | 132 | return true; |
|
128 | 133 | } |
|
129 | 134 | |
|
130 | 135 | bool CustomTableModel::removeRows ( int row, int count, const QModelIndex & parent) |
|
131 | 136 | { |
|
132 | 137 | if (row > this->rowCount() - 1) |
|
133 | 138 | return false; |
|
134 | 139 | if (row < 0) |
|
135 | 140 | row = 0; |
|
136 | 141 | if (row + count > rowCount()) |
|
137 | 142 | return false; |
|
138 | 143 | beginRemoveRows(parent, row, row + count - 1); |
|
139 | 144 | for (int i = row; i < row + count; i++) |
|
140 | 145 | { |
|
141 | 146 | m_points.removeAt(row); |
|
142 | 147 | m_labels.removeAt(row); |
|
143 | 148 | } |
|
144 | 149 | endRemoveRows(); |
|
145 | 150 | return true; |
|
146 | 151 | } |
General Comments 0
You need to be logged in to leave comments.
Login now