author | leseb <leseb> | 2002-07-06 20:45:39 (UTC) |
---|---|---|
committer | leseb <leseb> | 2002-07-06 20:45:39 (UTC) |
commit | 7c069ad1b76c681282499b157ef9b23ec000947d (patch) (unidiff) | |
tree | 677ae3903f6448a4f761e3a1245c7e4a746b8a1a | |
parent | 2517236d77e1f2a3d255c4c540a39f1661caaff9 (diff) | |
download | opie-7c069ad1b76c681282499b157ef9b23ec000947d.zip opie-7c069ad1b76c681282499b157ef9b23ec000947d.tar.gz opie-7c069ad1b76c681282499b157ef9b23ec000947d.tar.bz2 |
Fix DocLnk management to save documents correctly
-rw-r--r-- | noncore/apps/opie-sheet/mainwindow.cpp | 38 | ||||
-rw-r--r-- | noncore/apps/opie-sheet/mainwindow.h | 5 |
2 files changed, 20 insertions, 23 deletions
diff --git a/noncore/apps/opie-sheet/mainwindow.cpp b/noncore/apps/opie-sheet/mainwindow.cpp index efbcc20..09ee68b 100644 --- a/noncore/apps/opie-sheet/mainwindow.cpp +++ b/noncore/apps/opie-sheet/mainwindow.cpp | |||
@@ -44,73 +44,75 @@ | |||
44 | 44 | ||
45 | #define DEFAULT_NUM_ROWS 199 | 45 | #define DEFAULT_NUM_ROWS 199 |
46 | #define DEFAULT_NUM_COLS (26*3) | 46 | #define DEFAULT_NUM_COLS (26*3) |
47 | #define DEFAULT_NUM_SHEETS 3 | 47 | #define DEFAULT_NUM_SHEETS 3 |
48 | 48 | ||
49 | MainWindow::MainWindow() | 49 | MainWindow::MainWindow() |
50 | :QMainWindow() | 50 | :QMainWindow() |
51 | { | 51 | { |
52 | // initialize variables | 52 | // initialize variables |
53 | documentModified=FALSE; | 53 | documentModified=FALSE; |
54 | 54 | ||
55 | // construct objects | 55 | // construct objects |
56 | currentDoc=0; | ||
56 | fileSelector=new FileSelector("application/sheet-qt", this, QString::null); | 57 | fileSelector=new FileSelector("application/sheet-qt", this, QString::null); |
57 | connect(fileSelector, SIGNAL(closeMe()), this, SLOT(selectorHide())); | 58 | connect(fileSelector, SIGNAL(closeMe()), this, SLOT(selectorHide())); |
58 | connect(fileSelector, SIGNAL(newSelected(const DocLnk &)), this, SLOT(selectorFileNew(const DocLnk &))); | 59 | connect(fileSelector, SIGNAL(newSelected(const DocLnk &)), this, SLOT(selectorFileNew(const DocLnk &))); |
59 | connect(fileSelector, SIGNAL(fileSelected(const DocLnk &)), this, SLOT(selectorFileOpen(const DocLnk &))); | 60 | connect(fileSelector, SIGNAL(fileSelected(const DocLnk &)), this, SLOT(selectorFileOpen(const DocLnk &))); |
60 | 61 | ||
61 | listSheets.setAutoDelete(TRUE); | 62 | listSheets.setAutoDelete(TRUE); |
62 | 63 | ||
63 | initActions(); | 64 | initActions(); |
64 | initMenu(); | 65 | initMenu(); |
65 | initEditToolbar(); | 66 | initEditToolbar(); |
66 | initFunctionsToolbar(); | 67 | initFunctionsToolbar(); |
67 | initStandardToolbar(); | 68 | initStandardToolbar(); |
68 | initSheet(); | 69 | initSheet(); |
69 | 70 | ||
70 | // set window title | 71 | // set window title |
71 | setCaption(tr("Opie Sheet")); | 72 | setCaption(tr("Opie Sheet")); |
72 | 73 | ||
73 | // create sheets | 74 | // create sheets |
74 | selectorFileNew(currentDoc); | 75 | selectorFileNew(DocLnk()); |
75 | } | 76 | } |
76 | 77 | ||
77 | MainWindow::~MainWindow() | 78 | MainWindow::~MainWindow() |
78 | { | 79 | { |
80 | if (currentDoc) delete currentDoc; | ||
79 | } | 81 | } |
80 | 82 | ||
81 | void MainWindow::documentSave(DocLnk &lnkDoc) | 83 | void MainWindow::documentSave(DocLnk *lnkDoc) |
82 | { | 84 | { |
83 | FileManager fm; | 85 | FileManager fm; |
84 | QByteArray streamBuffer; | 86 | QByteArray streamBuffer; |
85 | QDataStream stream(streamBuffer, IO_WriteOnly); | 87 | QDataStream stream(streamBuffer, IO_WriteOnly); |
86 | 88 | ||
87 | typeSheet *currentSheet=findSheet(sheet->getName()); | 89 | typeSheet *currentSheet=findSheet(sheet->getName()); |
88 | if (!currentSheet) | 90 | if (!currentSheet) |
89 | { | 91 | { |
90 | QMessageBox::critical(this, tr("Error"), tr("Inconsistency error!")); | 92 | QMessageBox::critical(this, tr("Error"), tr("Inconsistency error!")); |
91 | return; | 93 | return; |
92 | } | 94 | } |
93 | sheet->copySheetData(¤tSheet->data); | 95 | sheet->copySheetData(¤tSheet->data); |
94 | stream.writeRawBytes("SQT100", 6); | 96 | stream.writeRawBytes("SQT100", 6); |
95 | stream << (Q_UINT32)listSheets.count(); | 97 | stream << (Q_UINT32)listSheets.count(); |
96 | for (typeSheet *tempSheet=listSheets.first(); tempSheet; tempSheet=listSheets.next()) | 98 | for (typeSheet *tempSheet=listSheets.first(); tempSheet; tempSheet=listSheets.next()) |
97 | { | 99 | { |
98 | stream << tempSheet->name << (Q_UINT32)tempSheet->data.count(); | 100 | stream << tempSheet->name << (Q_UINT32)tempSheet->data.count(); |
99 | for (typeCellData *tempCell=tempSheet->data.first(); tempCell; tempCell=tempSheet->data.next()) | 101 | for (typeCellData *tempCell=tempSheet->data.first(); tempCell; tempCell=tempSheet->data.next()) |
100 | stream << (Q_UINT32)tempCell->col << (Q_UINT32)tempCell->row << tempCell->borders.right << tempCell->borders.bottom << tempCell->background << (Q_UINT32)tempCell->alignment << tempCell->fontColor << tempCell->font << tempCell->data; | 102 | stream << (Q_UINT32)tempCell->col << (Q_UINT32)tempCell->row << tempCell->borders.right << tempCell->borders.bottom << tempCell->background << (Q_UINT32)tempCell->alignment << tempCell->fontColor << tempCell->font << tempCell->data; |
101 | } | 103 | } |
102 | 104 | ||
103 | lnkDoc.setType("application/sheet-qt"); | 105 | lnkDoc->setType("application/sheet-qt"); |
104 | if (!fm.saveFile(lnkDoc, streamBuffer)) | 106 | if (!fm.saveFile(*lnkDoc, streamBuffer)) |
105 | { | 107 | { |
106 | QMessageBox::critical(this, tr("Error"), tr("File cannot be saved!")); | 108 | QMessageBox::critical(this, tr("Error"), tr("File cannot be saved!")); |
107 | return; | 109 | return; |
108 | } | 110 | } |
109 | documentModified=FALSE; | 111 | documentModified=FALSE; |
110 | } | 112 | } |
111 | 113 | ||
112 | void MainWindow::documentOpen(const DocLnk &lnkDoc) | 114 | void MainWindow::documentOpen(const DocLnk &lnkDoc) |
113 | { | 115 | { |
114 | FileManager fm; | 116 | FileManager fm; |
115 | QByteArray streamBuffer; | 117 | QByteArray streamBuffer; |
116 | if (!lnkDoc.isValid() || !fm.loadFile(lnkDoc, streamBuffer)) | 118 | if (!lnkDoc.isValid() || !fm.loadFile(lnkDoc, streamBuffer)) |
@@ -163,76 +165,70 @@ void MainWindow::documentOpen(const DocLnk &lnkDoc) | |||
163 | } | 165 | } |
164 | } | 166 | } |
165 | } | 167 | } |
166 | 168 | ||
167 | int MainWindow::saveCurrentFile(bool ask=TRUE) | 169 | int MainWindow::saveCurrentFile(bool ask=TRUE) |
168 | { | 170 | { |
169 | if (ask) | 171 | if (ask) |
170 | { | 172 | { |
171 | int result=QMessageBox::information(this, tr("Save File"), tr("Do you want to save the current file?"), QMessageBox::Yes, QMessageBox::No, QMessageBox::Cancel); | 173 | int result=QMessageBox::information(this, tr("Save File"), tr("Do you want to save the current file?"), QMessageBox::Yes, QMessageBox::No, QMessageBox::Cancel); |
172 | if (result!=QMessageBox::Yes) return result; | 174 | if (result!=QMessageBox::Yes) return result; |
173 | } | 175 | } |
174 | 176 | ||
175 | if (currentDoc.name().isEmpty() || !currentDoc.isValid()) | 177 | if (!currentDoc->isValid()) |
176 | { | 178 | { |
177 | TextDialog dialogText(this); | 179 | TextDialog dialogText(this); |
178 | if (dialogText.exec(tr("Save File"), tr("&File Name:"), tr("UnnamedFile"))!=QDialog::Accepted || dialogText.getValue().isEmpty()) return QMessageBox::Cancel; | 180 | if (dialogText.exec(tr("Save File"), tr("&File Name:"), tr("UnnamedFile"))!=QDialog::Accepted || dialogText.getValue().isEmpty()) return QMessageBox::Cancel; |
179 | 181 | ||
180 | currentDoc.setName(dialogText.getValue()); | 182 | currentDoc->setName(dialogText.getValue()); |
183 | currentDoc->setFile(QString::null); | ||
184 | currentDoc->setLinkFile(QString::null); | ||
181 | } | 185 | } |
182 | 186 | ||
183 | documentSave(currentDoc); | 187 | documentSave(currentDoc); |
184 | return QMessageBox::Yes; | 188 | return QMessageBox::Yes; |
185 | } | 189 | } |
186 | 190 | ||
187 | void MainWindow::copyDocLnk(const DocLnk &source, DocLnk &target) | ||
188 | { | ||
189 | target.setName(source.name()); | ||
190 | target.setFile(source.file()); | ||
191 | target.setLinkFile(source.linkFile()); | ||
192 | target.setComment(source.comment()); | ||
193 | target.setType(source.type()); | ||
194 | target.setCategories(source.categories()); | ||
195 | } | ||
196 | |||
197 | void MainWindow::selectorFileNew(const DocLnk &lnkDoc) | 191 | void MainWindow::selectorFileNew(const DocLnk &lnkDoc) |
198 | { | 192 | { |
199 | selectorHide(); | 193 | selectorHide(); |
200 | 194 | ||
201 | if (documentModified && saveCurrentFile()==QMessageBox::Cancel) return; | 195 | if (documentModified && saveCurrentFile()==QMessageBox::Cancel) return; |
202 | copyDocLnk(lnkDoc, currentDoc); | 196 | if (currentDoc) delete currentDoc; |
197 | currentDoc = new DocLnk(lnkDoc); | ||
203 | listSheets.clear(); | 198 | listSheets.clear(); |
204 | comboSheets->clear(); | 199 | comboSheets->clear(); |
205 | 200 | ||
206 | typeSheet *newSheet=createNewSheet(); | 201 | typeSheet *newSheet=createNewSheet(); |
207 | newSheet->data.setAutoDelete(TRUE); | 202 | newSheet->data.setAutoDelete(TRUE); |
208 | sheet->setName(newSheet->name); | 203 | sheet->setName(newSheet->name); |
209 | sheet->setSheetData(&newSheet->data); | 204 | sheet->setSheetData(&newSheet->data); |
210 | for (int i=1; i<DEFAULT_NUM_SHEETS; ++i) | 205 | for (int i=1; i<DEFAULT_NUM_SHEETS; ++i) |
211 | createNewSheet(); | 206 | createNewSheet(); |
212 | documentModified=FALSE; | 207 | documentModified=FALSE; |
213 | } | 208 | } |
214 | 209 | ||
215 | void MainWindow::closeEvent(QCloseEvent *e) | 210 | void MainWindow::closeEvent(QCloseEvent *e) |
216 | { | 211 | { |
217 | if (documentModified && saveCurrentFile()==QMessageBox::Cancel) e->ignore(); | 212 | if (documentModified && saveCurrentFile()==QMessageBox::Cancel) e->ignore(); |
218 | else e->accept(); | 213 | else e->accept(); |
219 | } | 214 | } |
220 | 215 | ||
221 | void MainWindow::selectorFileOpen(const DocLnk &lnkDoc) | 216 | void MainWindow::selectorFileOpen(const DocLnk &lnkDoc) |
222 | { | 217 | { |
223 | selectorHide(); | 218 | selectorHide(); |
224 | 219 | ||
225 | if (documentModified && saveCurrentFile()==QMessageBox::Cancel) return; | 220 | if (documentModified && saveCurrentFile()==QMessageBox::Cancel) return; |
226 | copyDocLnk(lnkDoc, currentDoc); | 221 | if (currentDoc) delete currentDoc; |
222 | currentDoc = new DocLnk(lnkDoc); | ||
227 | listSheets.clear(); | 223 | listSheets.clear(); |
228 | comboSheets->clear(); | 224 | comboSheets->clear(); |
229 | 225 | ||
230 | documentOpen(lnkDoc); | 226 | documentOpen(lnkDoc); |
231 | documentModified=FALSE; | 227 | documentModified=FALSE; |
232 | } | 228 | } |
233 | 229 | ||
234 | void MainWindow::selectorShow() | 230 | void MainWindow::selectorShow() |
235 | { | 231 | { |
236 | sheet->hide(); | 232 | sheet->hide(); |
237 | setCentralWidget(fileSelector); | 233 | setCentralWidget(fileSelector); |
238 | fileSelector->show(); | 234 | fileSelector->show(); |
@@ -745,27 +741,29 @@ void MainWindow::slotColumnShow() | |||
745 | void MainWindow::slotColumnHide() | 741 | void MainWindow::slotColumnHide() |
746 | { | 742 | { |
747 | int row1, row2, col1, col2; | 743 | int row1, row2, col1, col2; |
748 | sheet->getSelection(&row1, &col1, &row2, &col2); | 744 | sheet->getSelection(&row1, &col1, &row2, &col2); |
749 | 745 | ||
750 | for (int col=col1; col<=col2; ++col) | 746 | for (int col=col1; col<=col2; ++col) |
751 | sheet->hideColumn(col); | 747 | sheet->hideColumn(col); |
752 | } | 748 | } |
753 | 749 | ||
754 | void MainWindow::slotFileSaveAs() | 750 | void MainWindow::slotFileSaveAs() |
755 | { | 751 | { |
756 | TextDialog dialogText(this); | 752 | TextDialog dialogText(this); |
757 | if (dialogText.exec(tr("Save File As"), tr("&File Name:"), currentDoc.name())!=QDialog::Accepted || dialogText.getValue().isEmpty()) return; | 753 | if (dialogText.exec(tr("Save File As"), tr("&File Name:"), currentDoc->name())!=QDialog::Accepted || dialogText.getValue().isEmpty()) return; |
758 | 754 | ||
759 | currentDoc.setName(dialogText.getValue()); | 755 | currentDoc->setName(dialogText.getValue()); |
756 | currentDoc->setFile(QString::null); | ||
757 | currentDoc->setLinkFile(QString::null); | ||
760 | documentSave(currentDoc); | 758 | documentSave(currentDoc); |
761 | } | 759 | } |
762 | 760 | ||
763 | void MainWindow::slotSheetRename() | 761 | void MainWindow::slotSheetRename() |
764 | { | 762 | { |
765 | TextDialog dialogText(this); | 763 | TextDialog dialogText(this); |
766 | if (dialogText.exec(tr("Rename Sheet"), tr("&Sheet Name:"), sheet->getName())!=QDialog::Accepted || dialogText.getValue().isEmpty()) return; | 764 | if (dialogText.exec(tr("Rename Sheet"), tr("&Sheet Name:"), sheet->getName())!=QDialog::Accepted || dialogText.getValue().isEmpty()) return; |
767 | QString newName=dialogText.getValue(); | 765 | QString newName=dialogText.getValue(); |
768 | 766 | ||
769 | typeSheet *tempSheet=findSheet(newName); | 767 | typeSheet *tempSheet=findSheet(newName); |
770 | if (tempSheet) | 768 | if (tempSheet) |
771 | { | 769 | { |
diff --git a/noncore/apps/opie-sheet/mainwindow.h b/noncore/apps/opie-sheet/mainwindow.h index 554e6f6..d68e25c 100644 --- a/noncore/apps/opie-sheet/mainwindow.h +++ b/noncore/apps/opie-sheet/mainwindow.h | |||
@@ -30,25 +30,25 @@ | |||
30 | 30 | ||
31 | typedef struct typeSheet | 31 | typedef struct typeSheet |
32 | { | 32 | { |
33 | QString name; | 33 | QString name; |
34 | QList<typeCellData> data; | 34 | QList<typeCellData> data; |
35 | }; | 35 | }; |
36 | 36 | ||
37 | class MainWindow: public QMainWindow | 37 | class MainWindow: public QMainWindow |
38 | { | 38 | { |
39 | Q_OBJECT | 39 | Q_OBJECT |
40 | 40 | ||
41 | // QPE objects | 41 | // QPE objects |
42 | DocLnk currentDoc; | 42 | DocLnk* currentDoc; |
43 | QPEMenuBar *menu; | 43 | QPEMenuBar *menu; |
44 | QPEToolBar *toolbarFunctions, *toolbarEdit, *toolbarStandard; | 44 | QPEToolBar *toolbarFunctions, *toolbarEdit, *toolbarStandard; |
45 | FileSelector *fileSelector; | 45 | FileSelector *fileSelector; |
46 | 46 | ||
47 | // QT objects | 47 | // QT objects |
48 | QPopupMenu *menuFile, *menuEdit, *menuInsert, *menuFormat, *menuData, *menuHelp, | 48 | QPopupMenu *menuFile, *menuEdit, *menuInsert, *menuFormat, *menuData, *menuHelp, |
49 | *submenuFunc, *submenuFuncStd, *submenuFuncMath, *submenuFuncStat, | 49 | *submenuFunc, *submenuFuncStd, *submenuFuncMath, *submenuFuncStat, |
50 | *submenuRow, *submenuCol, *submenuSheet; | 50 | *submenuRow, *submenuCol, *submenuSheet; |
51 | QAction *fileNew, *fileOpen, *fileSave, *fileSaveAs, *fileQuit, *helpAbout, *editAccept, *editCancel, *formatCells, | 51 | QAction *fileNew, *fileOpen, *fileSave, *fileSaveAs, *fileQuit, *helpAbout, *editAccept, *editCancel, *formatCells, |
52 | *funcPlus, *funcMinus, *funcCross, *funcDivide, *funcParanOpen, *funcParanClose, *funcComma, *funcEqual, | 52 | *funcPlus, *funcMinus, *funcCross, *funcDivide, *funcParanOpen, *funcParanClose, *funcComma, *funcEqual, |
53 | *editCut, *editCopy, *editPaste, *editPasteContents, *editClear, *insertCols, *insertRows, *insertSheets, *insertCells, | 53 | *editCut, *editCopy, *editPaste, *editPasteContents, *editClear, *insertCols, *insertRows, *insertSheets, *insertCells, |
54 | *rowHeight, *rowShow, *rowHide, *rowAdjust, *colWidth, *colShow, *colHide, *colAdjust, *sheetRename, *sheetRemove, | 54 | *rowHeight, *rowShow, *rowHide, *rowAdjust, *colWidth, *colShow, *colHide, *colAdjust, *sheetRename, *sheetRemove, |
@@ -67,26 +67,25 @@ class MainWindow: public QMainWindow | |||
67 | bool documentModified; | 67 | bool documentModified; |
68 | 68 | ||
69 | // Private functions | 69 | // Private functions |
70 | void initMenu(); | 70 | void initMenu(); |
71 | void initActions(); | 71 | void initActions(); |
72 | void initFunctionsToolbar(); | 72 | void initFunctionsToolbar(); |
73 | void initEditToolbar(); | 73 | void initEditToolbar(); |
74 | void initStandardToolbar(); | 74 | void initStandardToolbar(); |
75 | void initSheet(); | 75 | void initSheet(); |
76 | void addToData(const QString &data); | 76 | void addToData(const QString &data); |
77 | int saveCurrentFile(bool ask=TRUE); | 77 | int saveCurrentFile(bool ask=TRUE); |
78 | void documentOpen(const DocLnk &lnkDoc); | 78 | void documentOpen(const DocLnk &lnkDoc); |
79 | void copyDocLnk(const DocLnk &source, DocLnk &target); | 79 | void documentSave(DocLnk *lnkDoc); |
80 | void documentSave(DocLnk &lnkDoc); | ||
81 | void closeEvent(QCloseEvent *e); | 80 | void closeEvent(QCloseEvent *e); |
82 | void addFlyAction(const QString &text, const QString &menuText, const QString &tip, QWidget *w); | 81 | void addFlyAction(const QString &text, const QString &menuText, const QString &tip, QWidget *w); |
83 | typeSheet *createNewSheet(); | 82 | typeSheet *createNewSheet(); |
84 | typeSheet *findSheet(const QString &name); | 83 | typeSheet *findSheet(const QString &name); |
85 | 84 | ||
86 | private slots: | 85 | private slots: |
87 | void slotFileNew(); | 86 | void slotFileNew(); |
88 | void slotFileOpen(); | 87 | void slotFileOpen(); |
89 | void slotFileSave(); | 88 | void slotFileSave(); |
90 | void slotFileSaveAs(); | 89 | void slotFileSaveAs(); |
91 | void slotHelpAbout(); | 90 | void slotHelpAbout(); |
92 | void slotHelpGeneral(); | 91 | void slotHelpGeneral(); |