00001
00002
00003
00004
00005
00006
00007 #include "EngaugeAssert.h"
00008 #include "Logger.h"
00009 #include <QItemSelectionModel>
00010 #include <QMimeData>
00011 #include <QTableView>
00012 #include <QTextStream>
00013 #include "WindowModelBase.h"
00014 #include "WindowTable.h"
00015
00016 WindowModelBase::WindowModelBase () :
00017 m_view (0)
00018 {
00019 }
00020
00021 WindowModelBase::~WindowModelBase()
00022 {
00023 }
00024
00025 int WindowModelBase::fold2dIndexes (int row,
00026 int col,
00027 int rowLow,
00028 int colLow,
00029 int colHigh) const
00030 {
00031 return (row - rowLow) * (colHigh - colLow + 1) + (col - colLow);
00032 }
00033
00034 QMimeData *WindowModelBase::mimeData(const QModelIndexList &indexes) const
00035 {
00036 if (indexes.isEmpty ()) {
00037 return Q_NULLPTR;
00038 }
00039
00040 QMimeData *data = new QMimeData ();
00041
00042 data->setHtml (selectionAsHtml ());
00043 data->setText (selectionAsText (m_delimiter));
00044
00045 return data;
00046 }
00047
00048 QString WindowModelBase::selectionAsHtml () const
00049 {
00050 ENGAUGE_CHECK_PTR (m_view);
00051
00052 int rowLow, colLow, rowHigh, colHigh;
00053 QVector<QString> table;
00054
00055
00056 selectionAsTable (rowLow,
00057 colLow,
00058 rowHigh,
00059 colHigh,
00060 table);
00061
00062
00063 QString html;
00064 QTextStream str (&html);
00065
00066 str << "<table>";
00067 for (int row = rowLow; row <= rowHigh; row++) {
00068 str << "<tr>";
00069 for (int col = colLow; col <= colHigh; col++) {
00070 str << "<td>" << table [fold2dIndexes (row, col, rowLow, colLow, colHigh)] << "</td>";
00071 }
00072 str << "<tr>\n";
00073 }
00074 str << "</table>";
00075
00076 return html;
00077 }
00078
00079 void WindowModelBase::selectionAsTable (int &rowLow,
00080 int &colLow,
00081 int &rowHigh,
00082 int &colHigh,
00083 QVector<QString> &table) const
00084 {
00085
00086
00087
00088 QItemSelectionModel *selectionModel = m_view->selectionModel ();
00089 QModelIndexList selection = selectionModel->selectedIndexes ();
00090
00091 if (selection.size () > 0) {
00092
00093
00094 rowLow = 0;
00095 colLow = 0;
00096 rowHigh = 0;
00097 colHigh = 0;
00098 bool isFirst = true;
00099 for (QModelIndexList::const_iterator itr = selection.begin(); itr != selection.end(); itr++) {
00100 QModelIndex index = *itr;
00101 if (isFirst || index.row () < rowLow ) rowLow = index.row ();
00102 if (isFirst || index.row () > rowHigh) rowHigh = index.row ();
00103 if (isFirst || index.column () < colLow ) colLow = index.column ();
00104 if (isFirst || index.column () > colHigh) colHigh = index.column ();
00105 isFirst = false;
00106 }
00107
00108 int numRows = rowHigh - rowLow + 1;
00109 int numCols = colHigh - colLow + 1;
00110
00111
00112
00113 table.resize (numRows * numCols);
00114
00115 for (int i = 0; i < selection.size (); i++) {
00116 QModelIndex index = selection [i];
00117 QVariant d = data (index);
00118 QString text = d.toString ();
00119 table [fold2dIndexes (index.row(), index.column(), rowLow, colLow, colHigh)] = text;
00120 }
00121 } else {
00122
00123
00124 rowLow = 0;
00125 colLow = 0;
00126 rowHigh = -1;
00127 colHigh = -1;
00128
00129 }
00130 }
00131
00132 QString WindowModelBase::selectionAsText (ExportDelimiter delimiter) const
00133 {
00134 const bool NOT_GNUPLOT = false;
00135
00136 ENGAUGE_CHECK_PTR (m_view);
00137
00138 int rowLow, colLow, rowHigh, colHigh;
00139 QVector<QString> table;
00140
00141
00142 selectionAsTable (rowLow,
00143 colLow,
00144 rowHigh,
00145 colHigh,
00146 table);
00147
00148
00149 QString text;
00150 QTextStream str (&text);
00151 for (int row = rowLow; row <= rowHigh; row++) {
00152 QString delimiterStr;
00153 for (int col = colLow; col <= colHigh; col++) {
00154 str << delimiterStr << table [fold2dIndexes (row, col, rowLow, colLow, colHigh)];
00155 delimiterStr = exportDelimiterToText (delimiter,
00156 NOT_GNUPLOT);
00157 }
00158 str << "\n";
00159 }
00160
00161 return text;
00162 }
00163
00164 void WindowModelBase::setDelimiter (ExportDelimiter delimiter)
00165 {
00166 m_delimiter = delimiter;
00167 }
00168
00169 void WindowModelBase::setView (WindowTable &view)
00170 {
00171 m_view = &view;
00172 }