Thanks for your reply. I'm afraid your questions are starting to loose me....

I've pasted the last section of remaining code spreadsheet.cpp in the hope this will throw some light on the situation. The windowMenuAction is used in the addSpreadsheet function posted previously.
spreadsheet.cpp
Qt Code:
  1. #include <QtGui>
  2. #include "spreadsheet.h"
  3. #include "cell.h"
  4. #include <QTextDocument>
  5. Spreadsheet::Spreadsheet(QWidget *parent)
  6. : QTableWidget(parent)
  7. {
  8.  
  9. action = new QAction(this);
  10. action->setCheckable(true);
  11. connect(action, SIGNAL(triggered()), this, SLOT(show()));
  12. connect(action, SIGNAL(triggered()), this, SLOT(setFocus()));
  13.  
  14. isUntitled = true;
  15.  
  16. autoRecalc = true;
  17.  
  18. setItemPrototype(new Cell);
  19. setSelectionMode(ContiguousSelection);
  20.  
  21. connect(this, SIGNAL(itemChanged(QTableWidgetItem *)),
  22. this, SLOT(somethingChanged()));
  23. clear();
  24. setAttribute(Qt::WA_DeleteOnClose);
  25. }
  26. void Spreadsheet::newFile()
  27. {
  28. static int documentNumber = 1;
  29. curFile = tr("document%1.txt").arg(documentNumber);
  30. setWindowTitle(curFile + "[*]");
  31. action->setText(curFile);
  32. isUntitled = true;
  33. ++documentNumber;
  34. }
  35. QString Spreadsheet::currentLocation() const
  36. {
  37. return QChar('A' + currentColumn())
  38. + QString::number(currentRow() + 1);
  39. }
  40. QString Spreadsheet::currentFormula() const
  41. {
  42. return formula(currentRow(), currentColumn());
  43. }
  44. QTableWidgetSelectionRange Spreadsheet::selectedRange() const
  45. {
  46. QList<QTableWidgetSelectionRange> ranges = selectedRanges();
  47. if (ranges.isEmpty())
  48. return ranges.first();
  49. }
  50. void Spreadsheet::clear()
  51. {
  52. setRowCount(0);
  53. setColumnCount(0);
  54. setRowCount(RowCount);
  55. setColumnCount(ColumnCount);
  56. for (int i = 0; i < ColumnCount; ++i) {
  57. item->setText(QString(QChar('A' + i)));
  58. setHorizontalHeaderItem(i, item);
  59. }
  60. setCurrentCell(0, 0);
  61. }
  62. bool Spreadsheet::readFile(const QString &fileName)
  63. {
  64. QFile file(fileName);
  65. if (!file.open(QIODevice::ReadOnly)) {
  66. QMessageBox::warning(this, tr("Spreadsheet"),
  67. tr("Cannot read file %1:\n%2.")
  68. .arg(file.fileName())
  69. .arg(file.errorString()));
  70. return false;
  71. }
  72.  
  73. QDataStream in(&file);
  74. in.setVersion(QDataStream::Qt_4_3);
  75.  
  76. quint32 magic;
  77. in >> magic;
  78. if (magic != MagicNumber) {
  79. QMessageBox::warning(this, tr("Spreadsheet"),
  80. tr("The file is not a Spreadsheet file."));
  81. return false;
  82. }
  83.  
  84. clear();
  85.  
  86. quint16 row;
  87. quint16 column;
  88. QString str;
  89.  
  90. QApplication::setOverrideCursor(Qt::WaitCursor);
  91. while (!in.atEnd()) {
  92. in >> row >> column >> str;
  93. setFormula(row, column, str);
  94. }
  95. QApplication::restoreOverrideCursor();
  96. return true;
  97. }
  98.  
  99. bool Spreadsheet::writeFile(const QString &fileName)
  100. {
  101. QFile file(fileName);
  102. if (!file.open(QIODevice::WriteOnly)) {
  103. QMessageBox::warning(this, tr("Spreadsheet"),
  104. tr("Cannot write file %1:\n%2.")
  105. .arg(file.fileName())
  106. .arg(file.errorString()));
  107. return false;
  108. }
  109.  
  110. QDataStream out(&file);
  111. out.setVersion(QDataStream::Qt_4_3);
  112.  
  113. out << quint32(MagicNumber);
  114.  
  115. QApplication::setOverrideCursor(Qt::WaitCursor);
  116. for (int row = 0; row < RowCount; ++row) {
  117. for (int column = 0; column < ColumnCount; ++column) {
  118. QString str = formula(row, column);
  119. if (!str.isEmpty())
  120. out << quint16(row) << quint16(column) << str;
  121. }
  122. }
  123. QApplication::restoreOverrideCursor();
  124. return true;
  125. }
  126. void Spreadsheet::sort(const SpreadsheetCompare &compare)
  127. {
  128. QList<QStringList> rows;
  129. QTableWidgetSelectionRange range = selectedRange();
  130. int i;
  131.  
  132. for (i = 0; i < range.rowCount(); ++i) {
  133. for (int j = 0; j < range.columnCount(); ++j)
  134. row.append(formula(range.topRow() + i,
  135. range.leftColumn() + j));
  136. rows.append(row);
  137. }
  138.  
  139. qStableSort(rows.begin(), rows.end(), compare);
  140.  
  141. for (i = 0; i < range.rowCount(); ++i) {
  142. for (int j = 0; j < range.columnCount(); ++j)
  143. setFormula(range.topRow() + i, range.leftColumn() + j,
  144. rows[i][j]);
  145. }
  146.  
  147. clearSelection();
  148. somethingChanged();
  149. }
  150. void Spreadsheet::cut()
  151. {
  152. copy();
  153. del();
  154. }
  155. void Spreadsheet::copy()
  156. {
  157. QTableWidgetSelectionRange range = selectedRange();
  158. QString str;
  159.  
  160. for (int i = 0; i < range.rowCount(); ++i) {
  161. if (i > 0)
  162. str += "\n";
  163. for (int j = 0; j < range.columnCount(); ++j) {
  164. if (j > 0)
  165. str += "\t";
  166. str += formula(range.topRow() + i, range.leftColumn() + j);
  167. }
  168. }
  169. QApplication::clipboard()->setText(str);
  170. }
  171. void Spreadsheet::paste()
  172. {
  173. QTableWidgetSelectionRange range = selectedRange();
  174. QString str = QApplication::clipboard()->text();
  175. QStringList rows = str.split('\n');
  176. int numRows = rows.count();
  177. int numColumns = rows.first().count('\t') + 1;
  178.  
  179. if (range.rowCount() * range.columnCount() != 1
  180. && (range.rowCount() != numRows
  181. || range.columnCount() != numColumns)) {
  182. QMessageBox::information(this, tr("Spreadsheet"),
  183. tr("The information cannot be pasted because the copy "
  184. "and paste areas aren't the same size."));
  185. return;
  186. }
  187.  
  188. for (int i = 0; i < numRows; ++i) {
  189. QStringList columns = rows[i].split('\t');
  190. for (int j = 0; j < numColumns; ++j) {
  191. int row = range.topRow() + i;
  192. int column = range.leftColumn() + j;
  193. if (row < RowCount && column < ColumnCount)
  194. setFormula(row, column, columns[j]);
  195. }
  196. }
  197. somethingChanged();
  198. }
  199. void Spreadsheet::del()
  200. {
  201. QList<QTableWidgetItem *> items = selectedItems();
  202. if (!items.isEmpty()) {
  203. foreach (QTableWidgetItem *item, items)
  204. delete item;
  205. somethingChanged();
  206. }
  207. }
  208. void Spreadsheet::selectCurrentRow()
  209. {
  210. selectRow(currentRow());
  211. }
  212.  
  213. void Spreadsheet::selectCurrentColumn()
  214. {
  215. selectColumn(currentColumn());
  216. }
  217.  
  218. void Spreadsheet::recalculate()
  219. {
  220. for (int row = 0; row < RowCount; ++row) {
  221. for (int column = 0; column < ColumnCount; ++column) {
  222. if (cell(row, column))
  223. cell(row, column)->setDirty();
  224. }
  225. }
  226. viewport()->update();
  227. }
  228. void Spreadsheet::setAutoRecalculate(bool recalc)
  229. {
  230. autoRecalc = recalc;
  231. if (autoRecalc)
  232. recalculate();
  233. }
  234.  
  235.  
  236. void Spreadsheet::somethingChanged()
  237. {
  238. if (autoRecalc)
  239. recalculate();
  240. emit modified();
  241. }
  242.  
  243. Cell *Spreadsheet::cell(int row, int column) const
  244. {
  245. return static_cast<Cell *>(item(row, column));
  246. }
  247.  
  248. void Spreadsheet::setFormula(int row, int column,
  249. const QString &formula)
  250. {
  251. Cell *c = cell(row, column);
  252. if (!c) {
  253. c = new Cell;
  254. setItem(row, column, c);
  255. }
  256. c->setFormula(formula);
  257. }
  258.  
  259. QString Spreadsheet::formula(int row, int column) const
  260. {
  261. Cell *c = cell(row, column);
  262. if (c) {
  263. return c->formula();
  264. } else {
  265. return "";
  266. }
  267. }
  268.  
  269. QString Spreadsheet::text(int row, int column) const
  270. {
  271. Cell *c = cell(row, column);
  272. if (c) {
  273. return c->text();
  274. } else {
  275. return "";
  276. }
  277. }
  278.  
  279. bool SpreadsheetCompare::operator()(const QStringList &row1,
  280. const QStringList &row2) const
  281. {
  282. for (int i = 0; i < KeyCount; ++i) {
  283. int column = keys[i];
  284. if (column != -1) {
  285. if (row1[column] != row2[column]) {
  286. if (ascending[i]) {
  287. return row1[column] < row2[column];
  288. } else {
  289. return row1[column] > row2[column];
  290. }
  291. }
  292. }
  293. }
  294. return false;
  295. }
  296.  
  297.  
  298. void Spreadsheet::selectFont()
  299. {
  300. QList<QTableWidgetItem *> items = selectedItems();
  301. if (items.isEmpty())
  302. return;
  303.  
  304.  
  305. bool ok = false;
  306. QFont fnt = QFontDialog::getFont(&ok, font(), this);
  307.  
  308. if (!ok)
  309. return;
  310. foreach (QTableWidgetItem *item, items)
  311. if(item)
  312. item->setFont(fnt);
  313. somethingChanged();
  314.  
  315. }
  316.  
  317. void::Spreadsheet::toHtml(const QString &plainText)
  318. {
  319. QTableWidgetSelectionRange range = selectedRange();
  320. QString html= Qt::escape(plainText);
  321.  
  322. for (int i = 0; i < range.rowCount(); ++i) {
  323. if (i > 0)
  324. html.replace("\n", "\n<tr><td>");
  325. for (int j = 0; j < range.columnCount(); ++j) {
  326. if (j > 0)
  327. html.replace("\t", "\t<tr><td>");
  328.  
  329. html.prepend("<table>\n<tr><td>");
  330. html.append("\n</table>");
  331. }
  332. }
  333. printHtml(html);
  334.  
  335. }
  336. }
  337. Spreadsheet *Spreadsheet::openFile(const QString &fileName, QWidget *parent)
  338. {
  339. Spreadsheet *spreadsheet = new Spreadsheet(parent);
  340. if (spreadsheet->readFile(fileName))
  341. {
  342. spreadsheet->setCurrentFile(fileName);
  343. return spreadsheet;
  344. } else
  345. {
  346. delete spreadsheet;
  347. return 0;
  348. }
  349. }
  350. void Spreadsheet::setCurrentFile(const QString &fileName)
  351. {
  352. curFile = fileName;
  353. isUntitled = false;
  354. action->setText(strippedName(curFile));
  355. //document()->setModified(false);
  356. setWindowTitle(strippedName(curFile) + "[*]");
  357. setWindowModified(false);
  358. }
To copy to clipboard, switch view to plain text mode