| Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date | |
| All threads index page 1 | |
Hi group, I tested the connectSlotsByName mechanism with a simple example - it worked. Now I "transposed" the mechanism to my project: and it had problems. from the attached file the following is relevant: - resulttable.cpp, - resulttable.h - resulttable.ui or ui_resulttable.h moreover - main.cpp - mainwindow.cpp (is similar to the "spreadsheet" template from Blanchette book) - mainwindow.h - main.cpp Here is the problem: there is a slot defined in resulttable.h according to the "connectSlotByName"-mechanism. The pertaining signal comes from "calculatePushButton" within resulttable.ui or ui_resulttable.h The class "CResultTable" is defined within resulttable.h and it inherits from Ui::ResultForm of resulttable.ui the slot has the name of "on_calculatePushButton_clicked()" - the signal is defined within resulttable.ui. The strange thing is: this slot will not be addressed, when I click the Calculate pushbutton. At least it should output on "cout" a string (see code). When I alternatively reactivate the "connect - phrase" in resulttable.cpp (within the constructor) - it translates ok however crashed with sigsev ... this means for me: the address of the connect - routine has already been solved by the automatic "connectSlotByName" mechanism. When I connect this signal within the resulttable.ui (via designer) to the close slot of the form, everything works fine. Question: What is the reason that the slot will not be addressed when I generate the signal pressing the calculate-pushbutton ..?? Any comments? thanks in advance Jörg PS: F12 switches the two widgets from tableWidget (CGeoTable) to widget "CResulttable" where the calculate button is.
#include <QtGui>
#include <QtGui>
#include "cell.h"
#include "geotable.h"
CGeoTable::CGeoTable(int rows, int columns, QWidget *parent)
: QTableWidget( rows, columns, parent)
{
setItemPrototype(new Cell);
setSelectionMode(ContiguousSelection);
// connect(this, SIGNAL(itemChanged(QTableWidgetItem *)),
// this, SLOT(somethingChanged()));
clear();
showGrid = TRUE ; adjustSize() ;
setShowGrid( showGrid ) ;
setRowCount( rows ) ;
setColumnCount( columns ) ;
adjustSize() ;
}
QString CGeoTable::currentLocation() const
{
return QChar('A' + currentColumn())
+ QString::number(currentRow() + 1);
}
QString CGeoTable::currentFormula() const
{
return formula(currentRow(), currentColumn());
}
QTableWidgetSelectionRange CGeoTable::selectedRange() const
{
QList<QTableWidgetSelectionRange> ranges = selectedRanges();
if (ranges.isEmpty())
return QTableWidgetSelectionRange();
return ranges.first();
}
void CGeoTable::clear()
{
// setRowCount(0);
// setColumnCount(0);
setRowCount(RowCount);
setColumnCount(ColumnCount);
for (int i = 0; i < ColumnCount; ++i) {
QTableWidgetItem *item = new QTableWidgetItem;
item->setText(QString(QChar('A' + i)));
setHorizontalHeaderItem(i, item);
}
setCurrentCell(0, 0);
}
bool CGeoTable::readFile(const QString &fileName)
{
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox::warning(this, tr("CGeoTable"),
tr("Cannot read file %1:\n%2.")
.arg(file.fileName())
.arg(file.errorString()));
return false;
}
QDataStream in(&file);
in.setVersion(QDataStream::Qt_4_1);
quint32 magic;
in >> magic;
if (magic != MagicNumber) {
QMessageBox::warning(this, tr("CGeoTable"),
tr("The file is not a GeoTable file."));
return false;
}
clear();
quint16 row;
quint16 column;
QString str;
QApplication::setOverrideCursor(Qt::WaitCursor);
while (!in.atEnd()) {
in >> row >> column >> str;
//setFormula(row, column, str);
}
QApplication::restoreOverrideCursor();
return true;
}
bool CGeoTable::writeFile(const QString &fileName)
{
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly)) {
QMessageBox::warning(this, tr("CGeoTable"),
tr("Cannot write file %1:\n%2.")
.arg(file.fileName())
.arg(file.errorString()));
return false;
}
QDataStream out(&file);
out.setVersion(QDataStream::Qt_4_1);
out << quint32(MagicNumber);
QApplication::setOverrideCursor(Qt::WaitCursor);
for (int row = 0; row < RowCount; ++row) {
for (int column = 0; column < ColumnCount; ++column) {
QString str = formula(row, column);
if (!str.isEmpty())
out << quint16(row) << quint16(column) << str;
}
}
QApplication::restoreOverrideCursor();
return true;
}
void CGeoTable::sort(const CGeoTableCompare &compare)
{
QList<QStringList> rows;
QTableWidgetSelectionRange range = selectedRange();
int i;
for (i = 0; i < range.rowCount(); ++i) {
QStringList row;
for (int j = 0; j < range.columnCount(); ++j)
row.append(formula(range.topRow() + i,
range.leftColumn() + j));
rows.append(row);
}
qStableSort(rows.begin(), rows.end(), compare);
for (i = 0; i < range.rowCount(); ++i) {
for (int j = 0; j < range.columnCount(); ++j) ;
// setFormula(range.topRow() + i, range.leftColumn() + j,
// rows[i][j]);
}
clearSelection();
// somethingChanged();
}
void CGeoTable::cut()
{
copy();
del();
}
void CGeoTable::copy()
{
QTableWidgetSelectionRange range = selectedRange();
QString str;
for (int i = 0; i < range.rowCount(); ++i) {
if (i > 0)
str += "\n";
for (int j = 0; j < range.columnCount(); ++j) {
if (j > 0)
str += "\t";
str += formula(range.topRow() + i, range.leftColumn() + j);
}
}
QApplication::clipboard()->setText(str);
}
void CGeoTable::paste()
{
QTableWidgetSelectionRange range = selectedRange();
QString str = QApplication::clipboard()->text();
QStringList rows = str.split('\n');
int numRows = rows.count();
int numColumns = rows.first().count('\t') + 1;
if (range.rowCount() * range.columnCount() != 1
&& (range.rowCount() != numRows
|| range.columnCount() != numColumns)) {
QMessageBox::information(this, tr("CGeoTable"),
tr("The information cannot be pasted because the copy "
"and paste areas aren't the same size."));
return;
}
for (int i = 0; i < numRows; ++i) {
QStringList columns = rows[i].split('\t');
for (int j = 0; j < numColumns; ++j) {
int row = range.topRow() + i;
int column = range.leftColumn() + j;
if (row < RowCount && column < ColumnCount);
// setFormula(row, column, columns[j]);
}
}
// somethingChanged();
}
void CGeoTable::del()
{
foreach (QTableWidgetItem *item, selectedItems())
delete item;
}
void CGeoTable::selectCurrentRow()
{
selectRow(currentRow());
}
void CGeoTable::selectCurrentColumn()
{
selectColumn(currentColumn());
}
#ifdef OLD_CODE
void CGeoTable::recalculate()
{
for (int row = 0; row < RowCount; ++row) {
for (int column = 0; column < ColumnCount; ++column) {
if (cell(row, column))
cell(row, column)->setDirty();
}
}
viewport()->update();
}
void CGeoTable::setAutoRecalculate(bool recalc)
{
autoRecalc = recalc;
if (autoRecalc)
recalculate();
}
#endif
void CGeoTable::findNext(const QString &str, Qt::CaseSensitivity cs)
{
int row = currentRow();
int column = currentColumn() + 1;
while (row < RowCount) {
while (column < ColumnCount) {
if (text(row, column).contains(str, cs)) {
clearSelection();
setCurrentCell(row, column);
activateWindow();
return;
}
++column;
}
column = 0;
++row;
}
QApplication::beep();
}
void CGeoTable::findPrevious(const QString &str,
Qt::CaseSensitivity cs)
{
int row = currentRow();
int column = currentColumn() - 1;
while (row >= 0) {
while (column >= 0) {
if (text(row, column).contains(str, cs)) {
clearSelection();
setCurrentCell(row, column);
activateWindow();
return;
}
--column;
}
column = ColumnCount - 1;
--row;
}
QApplication::beep();
}
#ifdef OLD_CODE
void CGeoTable::somethingChanged()
{
if (autoRecalc)
recalculate();
emit modified();
}
#endif
Cell *CGeoTable::cell(int row, int column) const
{
return static_cast<Cell *>(item(row, column));
}
#ifdef OLD_CODE
void CGeoTable::setFormula(int row, int column,
const QString &formula)
{
Cell *c = cell(row, column);
if (!c) {
c = new Cell;
setItem(row, column, c);
}
c->setFormula(formula);
}
#endif
QString CGeoTable::formula(int row, int column) const
{
Cell *c = cell(row, column);
if (c) {
return c->formula();
} else {
return "";
}
}
QString CGeoTable::text(int row, int column) const
{
Cell *c = cell(row, column);
if (c) {
return c->text();
} else {
return "";
}
}
bool CGeoTableCompare::operator()(const QStringList &row1,
const QStringList &row2) const
{
for (int i = 0; i < KeyCount; ++i) {
int column = keys[i];
if (column != -1) {
if (row1[column] != row2[column]) {
if (ascending[i]) {
return row1[column] < row2[column];
} else {
return row1[column] > row2[column];
}
}
}
}
return false;
}
#ifndef CGEOTABLE_H
#ifndef CGEOTABLE_H
#define CGEOTABLE_H
#include <QTableWidget>
class Cell;
class CGeoTableCompare;
class CGeoTable : public QTableWidget
{
Q_OBJECT
public:
CGeoTable( int rows = 100, int columns = 4, QWidget *parent = 0);
QString currentLocation() const;
QString currentFormula() const;
QTableWidgetSelectionRange selectedRange() const;
void clear();
bool readFile(const QString &fileName);
bool writeFile(const QString &fileName);
void sort(const CGeoTableCompare &compare);
public slots:
void cut();
void copy();
void paste();
void del();
void selectCurrentRow();
void selectCurrentColumn();
void findNext(const QString &str, Qt::CaseSensitivity cs);
void findPrevious(const QString &str, Qt::CaseSensitivity cs);
signals:
void modified();
private:
enum { MagicNumber = 0x7F51C883, RowCount = 999, ColumnCount = 4 };
Cell *cell(int row, int column) const;
QString text(int row, int column) const;
QString formula(int row, int column) const;
bool showGrid ;
};
class CGeoTableCompare
{
public:
bool operator()(const QStringList &row1,
const QStringList &row2) const;
enum { KeyCount = 3 };
int keys[KeyCount];
bool ascending[KeyCount];
};
#endif
#include <QApplication>
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow mainWin;
mainWin.show();
return app.exec();
}
/*
/*
* (C)2005 Trolltech, Blanchette book,
* (C)2008 IBK-Consult, for Astronomy - sunrise project
****************************************************************
* mod. 2008-01-26 JK1 modified for sunrise QStackWindow etc.
*/
#include <QtGui>
#include "finddialog.h"
#include "gotocelldialog.h"
#include "mainwindow.h"
#include "sortdialog.h"
#include "geotable.h"
#include "ui_resulttable.h"
MainWindow::MainWindow()
{
// JK1 begin
geotable = new CGeoTable( 100, 4 ); // construct GeoTable
resulttable = new CResultTable( this ) ; // construct ResultForm ...
// incl. "connect's"
QWidget *resultWidget = new QWidget ;
// prepare for at least two pages:
// one is geotable
// the other is resulttable
stackedWidget = new QStackedWidget ; // here we start with
stackedWidget->addWidget( geotable ) ; // widget-stack
stackedWidget->addWidget( resultWidget ) ; // and have added two widgets to stack
// build main layouts ...
setCentralWidget( stackedWidget ); // set it as central widget ...
// JK 1 end
createActions();
createMenus();
createContextMenu();
createToolBars();
createStatusBar();
readSettings();
resulttable->setupUi( resultWidget ) ;
findDialog = 0;
setWindowIcon(QIcon(":/images/icon.png"));
setCurrentFile("");
}
void MainWindow::closeEvent(QCloseEvent *event)
{
if (okToContinue()) {
writeSettings();
event->accept();
} else {
event->ignore();
}
}
// File Menue options
void MainWindow::newFile()
{
if (okToContinue()) {
geotable->clear();
setCurrentFile("");
}
}
void MainWindow::open()
{
if (okToContinue()) {
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open geotable"), ".",
tr("geotable files (*.sp)"));
if (!fileName.isEmpty())
loadFile(fileName);
}
}
bool MainWindow::save()
{
if (curFile.isEmpty()) {
return saveAs();
} else {
return saveFile(curFile);
}
}
bool MainWindow::saveAs()
{
QString fileName = QFileDialog::getSaveFileName(this,
tr("Save Geotable"), ".",
tr("geotable files (*.sp)"));
if (fileName.isEmpty())
return false;
return saveFile(fileName);
}
void MainWindow::find()
{
if (!findDialog) {
findDialog = new FindDialog(this);
connect(findDialog, SIGNAL(findNext(const QString &,
Qt::CaseSensitivity)),
geotable, SLOT(findNext(const QString &,
Qt::CaseSensitivity)));
connect(findDialog, SIGNAL(findPrevious(const QString &,
Qt::CaseSensitivity)),
geotable, SLOT(findPrevious(const QString &,
Qt::CaseSensitivity)));
}
findDialog->show();
findDialog->activateWindow();
}
void MainWindow::goToCell()
{
GoToCellDialog dialog(this);
if (dialog.exec()) {
QString str = dialog.lineEdit->text().toUpper();
geotable->setCurrentCell(str.mid(1).toInt() - 1,
str[0].unicode() - 'A');
}
}
void MainWindow::toggleWindow() // toggles between data-widget and resultwidget
{
if( toggleIndex ) {
toggleIndex = 0 ;
}else {
toggleIndex = 1 ;
}
stackedWidget->setCurrentIndex( toggleIndex ) ;
}
void MainWindow::sort()
{
SortDialog dialog(this);
QTableWidgetSelectionRange range = geotable->selectedRange();
dialog.setColumnRange('A' + range.leftColumn(),
'A' + range.rightColumn());
if (dialog.exec()) {
CGeoTableCompare compare;
compare.keys[0] =
dialog.primaryColumnCombo->currentIndex();
compare.keys[1] =
dialog.secondaryColumnCombo->currentIndex() - 1;
compare.keys[2] =
dialog.tertiaryColumnCombo->currentIndex() - 1;
compare.ascending[0] =
(dialog.primaryOrderCombo->currentIndex() == 0);
compare.ascending[1] =
(dialog.secondaryOrderCombo->currentIndex() == 0);
compare.ascending[2] =
(dialog.tertiaryOrderCombo->currentIndex() == 0);
geotable->sort(compare);
}
}
void MainWindow::about()
{
QMessageBox::about(this, tr("About Sunrise"), //JK1
tr("<h2>Sunrise 0.1</h2>"
"<p>Copyright © 2008 IBK-Consult, D-31228 Peine"
"<p>Sunrise is a small application that "
"demonstrates times of sunrise and sunset at "
" various points on earth") ) ;
}
void MainWindow::openRecentFile()
{
if (okToContinue()) {
QAction *action = qobject_cast<QAction *>(sender());
if (action)
loadFile(action->data().toString());
}
}
void MainWindow::updateStatusBar()
{
locationLabel->setText(geotable->currentLocation());
formulaLabel->setText(geotable->currentFormula());
}
void MainWindow::spreadsheetModified()
{
setWindowModified(true);
updateStatusBar();
}
void MainWindow::createActions()
{
newAction = new QAction(tr("&New"), this);
newAction->setIcon(QIcon(":/images/new.png"));
newAction->setShortcut(tr("Ctrl+N"));
newAction->setStatusTip(tr("Create a new geotable file"));
connect(newAction, SIGNAL(triggered()), this, SLOT(newFile()));
openAction = new QAction(tr("&Open..."), this);
openAction->setIcon(QIcon(":/images/open.png"));
openAction->setShortcut(tr("Ctrl+O"));
openAction->setStatusTip(tr("Open an existing geotable file"));
connect(openAction, SIGNAL(triggered()), this, SLOT(open()));
saveAction = new QAction(tr("&Save"), this);
saveAction->setIcon(QIcon(":/images/save.png"));
saveAction->setShortcut(tr("Ctrl+S"));
saveAction->setStatusTip(tr("Save the geotable to disk"));
connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));
saveAsAction = new QAction(tr("Save &As..."), this);
saveAsAction->setStatusTip(tr("Save the geotable under a new "
"name"));
connect(saveAsAction, SIGNAL(triggered()), this, SLOT(saveAs()));
for (int i = 0; i < MaxRecentFiles; ++i) {
recentFileActions[i] = new QAction(this);
recentFileActions[i]->setVisible(false);
connect(recentFileActions[i], SIGNAL(triggered()),
this, SLOT(openRecentFile()));
}
exitAction = new QAction(tr("E&xit"), this);
exitAction->setShortcut(tr("Ctrl+Q"));
exitAction->setStatusTip(tr("Exit the application"));
connect(exitAction, SIGNAL(triggered()), this, SLOT(close())); // close is taken from QWidget
cutAction = new QAction(tr("Cu&t"), this);
cutAction->setIcon(QIcon(":/images/cut.png"));
cutAction->setShortcut(tr("Ctrl+X"));
cutAction->setStatusTip(tr("Cut the current selection's contents "
"to the clipboard"));
connect(cutAction, SIGNAL(triggered()), geotable, SLOT(cut()));
copyAction = new QAction(tr("&Copy"), this);
copyAction->setIcon(QIcon(":/images/copy.png"));
copyAction->setShortcut(tr("Ctrl+C"));
copyAction->setStatusTip(tr("Copy the current selection's contents "
"to the clipboard"));
connect(copyAction, SIGNAL(triggered()), geotable, SLOT(copy()));
pasteAction = new QAction(tr("&Paste"), this);
pasteAction->setIcon(QIcon(":/images/paste.png"));
pasteAction->setShortcut(tr("Ctrl+V"));
pasteAction->setStatusTip(tr("Paste the clipboard's contents into "
"the current selection"));
connect(pasteAction, SIGNAL(triggered()),
geotable, SLOT(paste()));
deleteAction = new QAction(tr("&Delete"), this);
deleteAction->setShortcut(tr("Del"));
deleteAction->setStatusTip(tr("Delete the current selection's "
"contents"));
connect(deleteAction, SIGNAL(triggered()),
geotable, SLOT(del()));
selectRowAction = new QAction(tr("&Row"), this);
selectRowAction->setStatusTip(tr("Select all the cells in the "
"current row"));
connect(selectRowAction, SIGNAL(triggered()),
geotable, SLOT(selectCurrentRow()));
#if OLD_CODE
selectColumnAction = new QAction(tr("&Column"), this);
selectColumnAction->setStatusTip(tr("Select all the cells in the "
"current column"));
connect(selectColumnAction, SIGNAL(triggered()),
geotable, SLOT(selectCurrentColumn()));
selectAllAction = new QAction(tr("&All"), this);
selectAllAction->setShortcut(tr("Ctrl+A"));
selectAllAction->setStatusTip(tr("Select all the cells in the "
"geotable"));
connect(selectAllAction, SIGNAL(triggered()),
geotable, SLOT(selectAll()));
#endif
findAction = new QAction(tr("&Find..."), this);
findAction->setIcon(QIcon(":/images/find.png"));
findAction->setShortcut(tr("Ctrl+F"));
findAction->setStatusTip(tr("Find a matching cell"));
connect(findAction, SIGNAL(triggered()), this, SLOT(find()));
goToCellAction = new QAction(tr("&Go to Cell..."), this);
goToCellAction->setIcon(QIcon(":/images/gotocell.png"));
goToCellAction->setShortcut(tr("F5"));
goToCellAction->setStatusTip(tr("Go to the specified cell"));
connect(goToCellAction, SIGNAL(triggered()),
this, SLOT(goToCell()));
showGridAction = new QAction(tr("&Show Grid"), this);
// JK1 begin
toggleWindowAction = new QAction(tr("&Toggle Window..."), this);
toggleWindowAction->setIcon(QIcon(":/images/gotocell.png"));
toggleWindowAction->setShortcut(tr("F12"));
toggleWindowAction->setStatusTip(tr("Toggle between Geodata-window and result window"));
toggleIndex = 0 ;
connect(toggleWindowAction, SIGNAL(triggered()),
this, SLOT(toggleWindow()));
// JK1 end
#if QT_VERSION < 0x040102
// workaround for a QTableWidget bug in Qt 4.1.1
connect(showGridAction, SIGNAL(toggled(bool)),
geotable->viewport(), SLOT(update()));
#endif
aboutAction = new QAction(tr("&About"), this);
aboutAction->setStatusTip(tr("Show the application's About box"));
connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
aboutQtAction = new QAction(tr("About &Qt"), this);
aboutQtAction->setStatusTip(tr("Show the Qt library's About box"));
connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
}
void MainWindow::createMenus()
{
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newAction);
fileMenu->addAction(openAction);
fileMenu->addAction(saveAction);
fileMenu->addAction(saveAsAction);
separatorAction = fileMenu->addSeparator();
for (int i = 0; i < MaxRecentFiles; ++i)
fileMenu->addAction(recentFileActions[i]);
fileMenu->addSeparator();
fileMenu->addAction(exitAction);
editMenu = menuBar()->addMenu(tr("&Edit"));
editMenu->addAction(cutAction);
editMenu->addAction(copyAction);
editMenu->addAction(pasteAction);
editMenu->addAction(deleteAction);
selectSubMenu = editMenu->addMenu(tr("&Select"));
selectSubMenu->addAction(selectRowAction);
editMenu->addSeparator();
editMenu->addAction(findAction);
editMenu->addAction(goToCellAction);
toolsMenu = menuBar()->addMenu(tr("&Tools"));
toolsMenu->addAction( toggleWindowAction ) ; // JK1
menuBar()->addSeparator();
helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(aboutAction);
helpMenu->addAction(aboutQtAction);
}
void MainWindow::createContextMenu()
{
geotable->addAction(cutAction);
geotable->addAction(copyAction);
geotable->addAction(pasteAction);
geotable->setContextMenuPolicy(Qt::ActionsContextMenu);
}
void MainWindow::createToolBars()
{
fileToolBar = addToolBar(tr("&File"));
fileToolBar->addAction(newAction);
fileToolBar->addAction(openAction);
fileToolBar->addAction(saveAction);
editToolBar = addToolBar(tr("&Edit"));
editToolBar->addAction(cutAction);
editToolBar->addAction(copyAction);
editToolBar->addAction(pasteAction);
editToolBar->addSeparator();
editToolBar->addAction(findAction);
editToolBar->addAction(goToCellAction);
}
void MainWindow::createStatusBar()
{
locationLabel = new QLabel(" W999 ");
locationLabel->setAlignment(Qt::AlignHCenter);
locationLabel->setMinimumSize(locationLabel->sizeHint());
formulaLabel = new QLabel;
formulaLabel->setIndent(3);
statusBar()->addWidget(locationLabel);
statusBar()->addWidget(formulaLabel, 1);
connect(geotable, SIGNAL(currentCellChanged(int, int, int, int)),
this, SLOT(updateStatusBar()));
connect(geotable, SIGNAL(modified()),
this, SLOT(spreadsheetModified()));
updateStatusBar();
}
void MainWindow::readSettings()
{
QSettings settings("Software Inc.", "geotable");
QRect rect = settings.value("geometry",
QRect(200, 200, 400, 400)).toRect();
move(rect.topLeft());
resize(rect.size());
recentFiles = settings.value("recentFiles").toStringList();
updateRecentFileActions();
bool showGrid = settings.value("showGrid", true).toBool();
showGridAction->setChecked(showGrid);
}
void MainWindow::writeSettings()
{
QSettings settings("Software Inc.", "geotable");
settings.setValue("geometry", geometry());
settings.setValue("recentFiles", recentFiles);
settings.setValue("showGrid", showGridAction->isChecked());
// settings.setValue("autoRecalc", autoRecalcAction->isChecked());
}
bool MainWindow::okToContinue()
{
if (isWindowModified()) {
int r = QMessageBox::warning(this, tr("Geotable"),
tr("The document has been modified.\n"
"Do you want to save your changes?"),
QMessageBox::Yes | QMessageBox::Default,
QMessageBox::No,
QMessageBox::Cancel | QMessageBox::Escape);
if (r == QMessageBox::Yes) {
return save();
} else if (r == QMessageBox::Cancel) {
return false;
}
}
return true;
}
bool MainWindow::loadFile(const QString &fileName)
{
if (!geotable->readFile(fileName)) {
statusBar()->showMessage(tr("Loading canceled"), 2000);
return false;
}
setCurrentFile(fileName);
statusBar()->showMessage(tr("File loaded"), 2000);
return true;
}
bool MainWindow::saveFile(const QString &fileName)
{
if (!geotable->writeFile(fileName)) {
statusBar()->showMessage(tr("Saving canceled"), 2000);
return false;
}
setCurrentFile(fileName);
statusBar()->showMessage(tr("File saved"), 2000);
return true;
}
void MainWindow::setCurrentFile(const QString &fileName)
{
curFile = fileName;
setWindowModified(false);
QString shownName = "Untitled";
if (!curFile.isEmpty()) {
shownName = strippedName(curFile);
recentFiles.removeAll(curFile);
recentFiles.prepend(curFile);
updateRecentFileActions();
}
setWindowTitle(tr("%1[*] - %2").arg(shownName)
.arg(tr("Sunrise")));
}
void MainWindow::updateRecentFileActions()
{
QMutableStringListIterator i(recentFiles);
while (i.hasNext()) {
if (!QFile::exists(i.next()))
i.remove();
}
for (int j = 0; j < MaxRecentFiles; ++j) {
if (j < recentFiles.count()) {
QString text = tr("&%1 %2")
.arg(j + 1)
.arg(strippedName(recentFiles[j]));
recentFileActions[j]->setText(text);
recentFileActions[j]->setData(recentFiles[j]);
recentFileActions[j]->setVisible(true);
} else {
recentFileActions[j]->setVisible(false);
}
}
separatorAction->setVisible(!recentFiles.isEmpty());
}
QString MainWindow::strippedName(const QString &fullFileName)
{
return QFileInfo(fullFileName).fileName();
}
/*
/*
* (C)2005 Trolltech, Blanchette book,
* (C)2008 IBK-Consult, for Astronomy - sunrise project
****************************************************************
* mod. 2008-01-26 JK1 modified for sunrise QStackWindow etc.
*/
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QStackedWidget>
#include "resulttable.h"
class QAction ;
class QLabel ;
class FindDialog ;
class CGeoTable ;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow() ;
protected:
void closeEvent( QCloseEvent *event ) ;
private slots:
void newFile() ;
void open() ;
bool save() ;
bool saveAs() ;
void find() ;
void goToCell() ;
void sort() ;
void about() ;
void openRecentFile() ;
void updateStatusBar() ;
void spreadsheetModified();
void toggleWindow() ; // JK1
private:
void createActions() ;
void createMenus() ;
void createContextMenu() ;
void createToolBars() ;
void createStatusBar() ;
void readSettings() ;
void writeSettings() ;
bool okToContinue() ;
bool loadFile( const QString &fileName ) ;
bool saveFile( const QString &fileName ) ;
void setCurrentFile( const QString &fileName ) ;
void updateRecentFileActions() ;
QString strippedName( const QString &fullFileName ) ;
// JK1 begin
// the stacked items (presently 2):
int toggleIndex ;
CGeoTable *geotable ; // index = 0
CResultTable *resulttable ; // index = 1
// JK1 end
FindDialog *findDialog ;
QLabel * locationLabel ;
QLabel * formulaLabel ;
QStringList recentFiles ;
QString curFile ;
QStackedWidget *stackedWidget ;
enum{ MaxRecentFiles = 5 } ;
QAction *recentFileActions[MaxRecentFiles] ;
QAction *separatorAction ;
QMenu *fileMenu ;
QMenu *editMenu ;
QMenu *selectSubMenu;
QMenu *toolsMenu;
QMenu *optionsMenu;
QMenu *helpMenu;
QToolBar *fileToolBar ;
QToolBar *editToolBar ;
QAction *newAction ;
QAction *openAction ;
QAction *saveAction;
QAction *saveAsAction;
QAction *exitAction;
QAction *cutAction;
QAction *copyAction;
QAction *pasteAction;
QAction *deleteAction;
QAction *selectRowAction;
QAction *selectColumnAction;
QAction *selectAllAction;
QAction *findAction;
QAction *goToCellAction;
QAction *showGridAction;
QAction *toggleWindowAction ;
QAction *aboutAction;
QAction *aboutQtAction;
} ;
#endif
/*
/*
* (C)2005 Trolltech, Blanchette book,
* (C)2008 IBK-Consult, for Astronomy - sunrise project
****************************************************************
* implementation module
*
* mod. 2008-01-26 JK1 modified for sunrise QStackWindow etc.
*/
#include <QtGui>
#include <iostream> // for test printouts on console ...
#include "resulttable.h"
CResultTable::CResultTable( QWidget *parent ):QWidget( parent )
{
//setupUi( this ) ; //not here! but in 'QMainWindow'
QWidget *resultTablePage = new QWidget ;
resultTablePage = resultTablePage ;
// connect( calculatePushButton, SIGNAL( clicked() ), // should not be executed
// parent, SLOT( on_calculatePushButton_clicked() ) ) ; // due to "connectSlotsByName()"
// mechanism - if executed
// then crash!
}
void CResultTable::on_calculatePushButton_clicked()
{
std::cout << "calculate Button pressed" << std::endl ;
}
/*
/*
* (C)2005 Trolltech, Blanchette book,
* (C)2008 IBK-Consult, for Astronomy - sunrise project
****************************************************************
* declaration module
* mod. 2008-01-26 JK1 modified for sunrise QStackWindow etc.
*/
#ifndef RESULT_TABLE_H
#define RESULT_TABLE_H
#include <QWidget>
#include <QLineEdit>
#include <QLabel>
#include <QString>
#include "ui_resulttable.h"
class CResultTable : public QWidget, public Ui::ResultForm
{
Q_OBJECT
public:
CResultTable( QWidget *parent = 0) ;
virtual ~CResultTable(){} // inline virtual destructor ...
public slots:
void on_calculatePushButton_clicked() ;
#ifdef NEW_CODE_TO_COME_LATER
private:
QString
latString,
lonString ,
dayString,
yearString ,
latDegString ,
lonDegString ,
latMinString ,
lonMinString ,
latSecString ,
lonSecString ;
int neg ,
lpyr ;
double JD ,
year ,
mn ,
angleRad ,
angleDeg ,
juld ,
month ,
day ;
bool isValidInput( QString latString, QString lonString,
QString dayString, QString yearString ) ;
double radToDeg( double angleRad ) ;
double degToRad( double angleDeg ) ;
double calcDayOfYear( double mn, double dy, int lpyr ) ;
QString calcDayOfWeek( double juld ) ;
double calcJD( double year, double month, double day ) ;
QString calcDateFromJD( double JD ) ;
QString calcDayFromJD( double jd ) ;
double calcTimeJulianCent( double jd ) ; // returns Julian Century
double calcJDFromJulianCent( double t ) ; // returns Julian Day
#endif
};
#endif
Attachment:
Attachment:
resulttable.ui
Description: application/designer