00001 #include <QTableWidget>
00002 #include <QMenu>
00003
00004 #include "qsecolumn.h"
00005 #include "qsescan.h"
00006 #include "qsedataset.h"
00007 #include "qsedataexport.h"
00008 #include "qseaveragerrole.h"
00009 #include "summarytablecontroller.h"
00010 #include "coloredtableitem.h"
00011
00012 SummaryTableController::SummaryTableController
00013 (QseDataSet *ds, QseAveragerRole *r,
00014 QTableWidget *tw, QObject *parent)
00015 : QObject(parent),
00016 m_DataSet(ds),
00017 m_Role(r),
00018 m_Table(tw)
00019 {
00020
00021
00022 m_Table->setContextMenuPolicy(Qt::CustomContextMenu);
00023
00024 connect(m_Table, SIGNAL(customContextMenuRequested(const QPoint &)),
00025 this, SLOT(summaryTableContextMenu(const QPoint &)));
00026 }
00027
00028 SummaryTableController::~SummaryTableController()
00029 {
00030 }
00031
00032
00033 void SummaryTableController::updateSummaryTable()
00034 {
00035 return;
00036
00037 int nscans = m_DataSet -> scanCount();
00038
00039 updateSummaryTableHeaders();
00040 updateSummaryTableScanInfo(0, nscans);
00041 updateSummaryTableColumnInfo(0, nscans);
00042 }
00043
00044 void SummaryTableController::updateSummaryTableHeaders()
00045 {
00046 int ns = m_DataSet->scanCount();
00047 int mxnc = m_DataSet->maxColumnCount();
00048
00049 m_Table -> setRowCount(ns);
00050 m_Table -> setColumnCount(mxnc + EXTRACOLUMNCOUNT);
00051
00052 m_Table -> setHorizontalHeaderItem(0, new ColoredTableItem(Qt::black, false, "Command"));
00053 m_Table -> setHorizontalHeaderItem(1, new ColoredTableItem(Qt::black, false, "#Cols"));
00054 m_Table -> setHorizontalHeaderItem(2, new ColoredTableItem(Qt::black, false, "#Points"));
00055 m_Table -> setHorizontalHeaderItem(3, new ColoredTableItem(Qt::black, false, "Status"));
00056
00057 for (int i = 0; i < mxnc; i++) {
00058 m_Table -> setHorizontalHeaderItem(i+EXTRACOLUMNCOUNT,
00059 new ColoredTableItem(Qt::black, false, QString("col%1").arg(i + 1)));
00060 }
00061 }
00062
00063 void SummaryTableController::updateSummaryTableScanInfo(int scan0, int scan1)
00064 {
00065 for (int i = scan0; i <= scan1; i++) {
00066 QseScan *s = m_DataSet->scan(i);
00067 if (s) {
00068 m_Table -> setItem(i, 0, m_Role->styledScanItem(s));
00069 m_Table -> setItem(i, 1, new ColoredTableItem(Qt::black, false,
00070 QString("%1").arg(s->columnCount())));
00071 m_Table -> setItem(i, 2, new ColoredTableItem(Qt::black, false,
00072 QString("%1").arg(s->maxRowCount())));
00073 m_Table -> setItem(i, 3, new ColoredTableItem(Qt::black, false, ""));
00074 }
00075 }
00076 }
00077
00078 void SummaryTableController::updateSummaryTableColumnInfo(int scan0, int scan1)
00079 {
00080 updateSummaryTableScanInfo(scan0, scan1);
00081 updateSummaryTableColumnInfo(scan0, scan1, 0, m_DataSet->maxColumnCount());
00082 }
00083
00084 void SummaryTableController::updateSummaryTableColumnInfo(int scan0, int scan1, int col0, int col1)
00085 {
00086 m_Table -> setUpdatesEnabled(false);
00087
00088 for (int i = scan0; i<=scan1; i++) {
00089 QseScan *s = m_DataSet->scan(i);
00090 if (s) {
00091 for (int c = col0; c <= col1; c++) {
00092 QseColumn *col = s->column(c);
00093
00094 if (col) {
00095 m_Table -> setItem(i, c + EXTRACOLUMNCOUNT, m_Role->styledColumnItem(col));
00096 }
00097 }
00098 }
00099 }
00100
00101 m_Table -> resizeColumnsToContents();
00102 m_Table -> resizeRowsToContents();
00103
00104 m_Table -> setUpdatesEnabled(true);
00105 m_Table -> repaint();
00106 }
00107
00108 void SummaryTableController::updateSummaryTableSizes()
00109 {
00110 m_Table -> setUpdatesEnabled(false);
00111
00112 m_Table -> resizeColumnsToContents();
00113 m_Table -> resizeRowsToContents();
00114
00115 m_Table -> setUpdatesEnabled(true);
00116 m_Table -> repaint();
00117 }
00118
00119 void SummaryTableController::updateSummaryTableScans(int scan0, int scan1)
00120 {
00121 int tnr = m_Table ->rowCount();
00122 int nscans = m_DataSet->scanCount();
00123
00124 if (scan0<0) {
00125 m_Table -> setColumnCount(EXTRACOLUMNCOUNT);
00126 m_Table -> setRowCount(0);
00127 m_Table -> setHorizontalHeaderItem(0, new ColoredTableItem(Qt::black, false, "Command"));
00128 m_Table -> setHorizontalHeaderItem(1, new ColoredTableItem(Qt::black, false, "#Cols"));
00129 m_Table -> setHorizontalHeaderItem(2, new ColoredTableItem(Qt::black, false, "#Points"));
00130 m_Table -> setHorizontalHeaderItem(3, new ColoredTableItem(Qt::black, false, "Status"));
00131 } else if (scan1 > tnr) {
00132 m_Table -> setRowCount(scan1);
00133 int tnc = m_Table -> columnCount() - EXTRACOLUMNCOUNT;
00134
00135 for (int i = scan0; i<=scan1; i++) {
00136 QseScan *s = m_DataSet->scan(i);
00137 if (s) {
00138 int snc = s->columnCount();
00139
00140 if (snc > tnc) {
00141 m_Table -> setColumnCount(snc + EXTRACOLUMNCOUNT);
00142
00143 for (int c = tnc; c < snc; c++) {
00144 m_Table -> setHorizontalHeaderItem(c+EXTRACOLUMNCOUNT,
00145 new ColoredTableItem(Qt::black, false,
00146 QString("col%1").arg(c + 1)));
00147 }
00148
00149 tnc = snc;
00150 }
00151
00152 m_Table -> setItem(i, 0, m_Role -> styledScanItem(s));
00153 m_Table -> setItem(i, 1, new ColoredTableItem(Qt::black, false,
00154 QString("%1").arg(s->columnCount())));
00155 m_Table -> setItem(i, 2, new ColoredTableItem(Qt::black, false,
00156 QString("%1").arg(s->maxRowCount())));
00157 m_Table -> setItem(i, 3, new ColoredTableItem(Qt::black, false, ""));
00158
00159 for (int c = 0; c < snc; c++) {
00160 QseColumn *col = s->column(c);
00161
00162 if (col) {
00163 m_Table -> setItem(i, c + EXTRACOLUMNCOUNT, m_Role -> styledColumnItem(col));
00164 }
00165 }
00166 }
00167 }
00168 }
00169 }
00170
00171 void SummaryTableController::exportSelectedData(QList<QTableWidgetSelectionRange> selections)
00172 {
00173 QList<int> selectedscans;
00174 QTableWidgetSelectionRange selection;
00175
00176 foreach(selection, selections) {
00177 int minrow = selection.topRow();
00178 int maxrow = selection.bottomRow();
00179 for (int row = minrow; row <= maxrow; row++) {
00180 selectedscans.append(row);
00181 }
00182 }
00183
00184 QseDataExport::exportSelectedData("export/data", m_DataSet, selectedscans);
00185 }
00186
00187 void SummaryTableController::summaryTableContextMenu(const QPoint & pos)
00188 {
00189 QList<QTableWidgetSelectionRange> selections = m_Table -> selectedRanges();
00190 QTableWidgetSelectionRange selection;
00191
00192 int col = m_Table->columnAt(pos.x());
00193
00194 if (col < EXTRACOLUMNCOUNT) {
00195 QMenu menu(NULL, NULL);
00196 QAction* revertItem = menu.addAction(QString("Revert scan(s) to Default Usage"));
00197 QAction* unusedItem = menu.addAction(QString("Do Not Use scan(s)"));
00198 QAction* usedItem = menu.addAction(QString("Use scan(s)"));
00199 menu.addSeparator();
00200 QAction* exportItem = menu.addAction(QString("Export scan(s)..."));
00201
00202 QseScan::QseScanRole newrole = QseScan::UnspecifiedRole;
00203
00204 menu.setMouseTracking(TRUE);
00205
00206 QAction* init = revertItem;
00207 QAction* id = menu.exec(QCursor::pos() + QPoint( -40, -10), init);
00208
00209 if (id) {
00210 if (id == exportItem) {
00211 exportSelectedData(selections);
00212 } else {
00213 if (id == revertItem) {
00214 newrole = QseScan::UnspecifiedRole;
00215 } else if (id == unusedItem) {
00216 newrole = QseScan::NotUsedRole;
00217 } else if (id == usedItem) {
00218 newrole = QseScan::UsedRole;
00219 }
00220
00221 foreach(selection, selections) {
00222 int nchanged = 0;
00223 int minrow = selection.topRow();
00224 int maxrow = selection.bottomRow();
00225 for (int row = minrow; row <= maxrow; row++) {
00226 QseScan *s = m_DataSet -> scan(row);
00227
00228 if (s) {
00229 nchanged++;
00230 s -> setScanRole(newrole);
00231 }
00232 }
00233
00234 if (nchanged) {
00235 emit scanRangeRoleChanged(minrow, maxrow);
00236 }
00237 }
00238 }
00239 }
00240 } else if (col >= EXTRACOLUMNCOUNT) {
00241 QMenu menu(NULL, NULL);
00242
00243 QAction* revertItem = menu.addAction(QString("Revert column(s) to Default Usage"));
00244 QAction* unusedItem = menu.addAction(QString("Do Not Use column(s)"));
00245 QAction* detectorItem = menu.addAction(QString("Use column(s) as a signal channel"));
00246 QAction* normalizationItem = menu.addAction(QString("Use column(s) as a normalization channel"));
00247 QAction* energyItem = menu.addAction(QString("Use column(s) as Energy (X Value)"));
00248 QAction* secondsItem = menu.addAction(QString("Use column(s) as counting time"));
00249 menu.addSeparator();
00250 QAction* exportItem = menu.addAction(QString("Export scan(s)..."));
00251
00252 QseColumn::QseColumnRole newrole = QseColumn::UnspecifiedRole;
00253
00254 menu.setMouseTracking(TRUE);
00255
00256 QAction* init = revertItem;
00257 QAction* id = menu.exec(QCursor::pos() + QPoint( -40, -10), init);
00258
00259 if (id) {
00260 if (id == exportItem) {
00261 exportSelectedData(selections);
00262 } else {
00263 if (id == revertItem) {
00264 newrole = QseColumn::UnspecifiedRole;
00265 } else if (id == unusedItem) {
00266 newrole = QseColumn::NotUsedRole;
00267 } else if (id == detectorItem) {
00268 newrole = QseColumn::DetectorRole;
00269 } else if (id == normalizationItem) {
00270 newrole = QseColumn::NormalizationRole;
00271 } else if (id == energyItem) {
00272 newrole = QseColumn::EnergyRole;
00273 } else if (id == secondsItem) {
00274 newrole = QseColumn::SecondsRole;
00275 }
00276
00277 foreach(selection, selections) {
00278 int minrow = selection.topRow();
00279 int maxrow = selection.bottomRow();
00280 int mincol = selection.leftColumn()-EXTRACOLUMNCOUNT;
00281 int maxcol = selection.rightColumn()-EXTRACOLUMNCOUNT;
00282
00283 int nchanged = 0;
00284
00285 for (int row = minrow; row <= maxrow; row++) {
00286 QseScan *s = m_DataSet -> scan(row);
00287
00288 if (s) {
00289 for (int col=mincol; col<=maxcol; col++) {
00290 if ((col >= 0) && (col <= s->columnCount())) {
00291 QseColumn *c = s->column(col);
00292
00293 if (c) {
00294 c->setColumnRole(newrole);
00295 }
00296 }
00297 }
00298
00299 nchanged++;
00300 }
00301 }
00302
00303 if (nchanged) {
00304 emit columnRangeRolesChanged(minrow, maxrow, mincol, maxcol);
00305 }
00306 }
00307 }
00308 }
00309 }
00310 }
00311