AddressBook.cpp
#include <TCtrlLib/TCtrlLib.h>
#define LAYOUTFILE <AddressBook/AddressBook.lay>
#include <CtrlCore/lay.h>
class AddressBook : public WithAddressBookLayout<TopWindow> {
WithModifyLayout<StaticRect> modify;
WithSearchLayout<StaticRect> search;
FileSel fs;
String filename;
void SetupSearch();
void Add();
void Change();
void Search();
void Open();
void Save();
void SaveAs();
void Print();
void Quit();
void FileMenu(Bar& bar);
void MainMenu(Bar& bar);
typedef AddressBook CLASSNAME;
public:
void Serialize(Stream& s);
AddressBook();
};
AddressBook::AddressBook()
{
CtrlLayout(*this, "Address book");
CtrlLayout(modify);
CtrlLayout(search);
tab.Add(modify, "Modify");
tab.Add(search, "Search");
ActiveFocus(search.name);
search.oname = true;
search.oname <<= search.osurname <<= search.oaddress
<<= search.oemail <<= THISBACK(SetupSearch);
array.AddColumn("Name");
array.AddColumn("Surname");
array.AddColumn("Address");
array.AddColumn("Email");
modify.add <<= THISBACK(Add);
modify.change <<= THISBACK(Change);
search.search <<= THISBACK(Search);
SetupSearch();
fs.AllFilesType();
menu.Set(THISBACK(MainMenu));
}
void AddressBook::FileMenu(Bar& bar)
{
bar.Add("Open..", CtrlImg::open(), THISBACK(Open));
bar.Add("Save", CtrlImg::save(), THISBACK(Save));
bar.Add("Save as..", CtrlImg::save_as(), THISBACK(SaveAs));
bar.Separator();
bar.Add("Print", CtrlImg::print(), THISBACK(Print));
bar.Separator();
bar.Add("Quit", THISBACK(Quit));
}
void AddressBook::MainMenu(Bar& bar)
{
bar.Add("File", THISBACK(FileMenu));
}
void AddressBook::SetupSearch()
{
search.name.Enable(search.oname);
search.surname.Enable(search.osurname);
search.address.Enable(search.oaddress);
search.email.Enable(search.oemail);
}
void AddressBook::Add()
{
array.Add(~modify.name, ~modify.surname, ~modify.address, ~modify.email);
array.GoEnd();
modify.name <<= modify.surname <<= modify.address <<= modify.email <<= Null;
ActiveFocus(modify.name);
}
void AddressBook::Change()
{
if(array.IsCursor()) {
array.Set(0, ~modify.name);
array.Set(1, ~modify.surname);
array.Set(2, ~modify.address);
array.Set(3, ~modify.email);
}
}
bool Contains(const String& text, const String& substr)
{
for(const char *s = text; s <= text.End() - substr.GetLength(); s++)
if(strncmp(s, substr, substr.GetLength()) == 0)
return true;
return false;
}
void AddressBook::Search()
{
if(!array.GetCount()) return;
bool sc = true;
array.ClearSelection();
for(int i = 0; i < array.GetCount(); i++) {
if((!search.oname || Contains(array.Get(i, 0), ~search.name)) &&
(!search.osurname || Contains(array.Get(i, 1), ~search.surname)) &&
(!search.oaddress || Contains(array.Get(i, 2), ~search.address)) &&
(!search.oemail || Contains(array.Get(i, 3), ~search.email))) {
array.Select(i);
if(sc) {
array.SetCursor(i);
array.CenterCursor();
sc = false;
};
}
}
}
void AddressBook::Open()
{
if(!fs.ExecuteOpen()) return;
filename = fs;
FileIn in(filename);
if(!in) {
Exclamation("Unable to open " + filename);
return;
}
array.Clear();
while(!in.IsEof()) {
Vector<Value> q;
for(int i = 0; i < 4; i++)
q.Add(in.GetLine());
array.Add(q);
}
}
void AddressBook::Save()
{
if(IsEmpty(filename)) {
SaveAs();
return;
}
FileOut out(filename);
if(!out) {
Exclamation("Unable to open " + filename);
return;
}
for(int i = 0; i < array.GetCount(); i++) {
for(int q = 0; q < 4; i++)
out.PutLine(String(array.Get(i, q)));
}
}
void AddressBook::SaveAs()
{
if(!fs.ExecuteSaveAs()) return;
filename = fs;
Save();
}
void AddressBook::Print()
{
String qtf;
qtf = "++Name||Surname||Address||Email";
for(int i = 0; i < array.GetCount(); i++) {
qtf << "--";
for(int q = 0; q < 4; q++) {
if(q) qtf << "||";
qtf << DeQtf((String)array.Get(i, q));
}
}
qtf << "++";
Report report;
report << qtf;
report.Perform();
}
void AddressBook::Quit()
{
Break();
}
void AddressBook::Serialize(Stream& s)
{
int version = 0;
s / version;
s % search.oname % search.osurname % search.oaddress % search.oemail;
s % fs;
SetupSearch();
}
GUI_APP_MAIN
{
AddressBook ab;
LoadFromFile(ab);
ab.Run();
StoreToFile(ab);
}
|
mainwindow.h
#ifndef AB_MAINWINDOW_H
#define AB_MAINWINDOW_H
#include <qmainwindow.h>
#include <qstring.h>
class QToolBar;
class QPopupMenu;
class ABCentralWidget;
class ABMainWindow: public QMainWindow
{
Q_OBJECT
public:
ABMainWindow();
~ABMainWindow();
protected slots:
void fileNew();
void fileOpen();
void fileSave();
void fileSaveAs();
void filePrint();
void closeWindow();
protected:
void setupMenuBar();
void setupFileTools();
void setupStatusBar();
void setupCentralWidget();
QToolBar *fileTools;
QString filename;
ABCentralWidget *view;
};
#endif
mainwindow.cpp
#include "mainwindow.h"
#include "centralwidget.h"
#include <qtoolbar.h>
#include <qtoolbutton.h>
#include <qpopupmenu.h>
#include <qmenubar.h>
#include <qstatusbar.h>
#include <qapplication.h>
#include <qfiledialog.h>
ABMainWindow::ABMainWindow()
: QMainWindow( 0, "example addressbook application" ),
filename( QString::null )
{
setupMenuBar();
setupFileTools();
setupStatusBar();
setupCentralWidget();
}
ABMainWindow::~ABMainWindow()
{
}
void ABMainWindow::setupMenuBar()
{
QPopupMenu *file = new QPopupMenu( this );
menuBar()->insertItem( "&File", file );
file->insertItem( "New", this, SLOT( fileNew() ), CTRL + Key_N );
file->insertItem( QPixmap( "fileopen.xpm" ), "Open", this, SLOT( fileOpen() ), CTRL + Key_O );
file->insertSeparator();
file->insertItem( QPixmap( "filesave.xpm" ), "Save", this, SLOT( fileSave() ), CTRL + Key_S );
file->insertItem( "Save As...", this, SLOT( fileSaveAs() ) );
file->insertSeparator();
file->insertItem( QPixmap( "fileprint.xpm" ), "Print...", this, SLOT( filePrint() ), CTRL + Key_P );
file->insertSeparator();
file->insertItem( "Close", this, SLOT( closeWindow() ), CTRL + Key_W );
file->insertItem( "Quit", qApp, SLOT( quit() ), CTRL + Key_Q );
}
void ABMainWindow::setupFileTools()
{
//fileTools = new QToolBar( this, "file operations" );
}
void ABMainWindow::setupStatusBar()
{
//statusBar()->message( "Ready", 2000 );
}
void ABMainWindow::setupCentralWidget()
{
view = new ABCentralWidget( this );
setCentralWidget( view );
}
void ABMainWindow::closeWindow()
{
close();
}
void ABMainWindow::fileNew()
{
}
void ABMainWindow::fileOpen()
{
QString fn = QFileDialog::getOpenFileName( QString::null, QString::null, this );
if ( !fn.isEmpty() ) {
filename = fn;
view->load( filename );
}
}
void ABMainWindow::fileSave()
{
if ( filename.isEmpty() ) {
fileSaveAs();
return;
}
view->save( filename );
}
void ABMainWindow::fileSaveAs()
{
QString fn = QFileDialog::getSaveFileName( QString::null, QString::null, this );
if ( !fn.isEmpty() ) {
filename = fn;
fileSave();
}
}
void ABMainWindow::filePrint()
{
}
centralwidget.h
#ifndef AB_CENTRALWIDGET_H
#define AB_CENTRALWIDGET_H
#include <qwidget.h>
#include <qstring.h>
class QTabWidget;
class QListView;
class QGridLayout;
class QLineEdit;
class QPushButton;
class QListViewItem;
class QCheckBox;
class ABCentralWidget : public QWidget
{
Q_OBJECT
public:
ABCentralWidget( QWidget *parent, const char *name = 0 );
void save( const QString &filename );
void load( const QString &filename );
protected slots:
void addEntry();
void changeEntry();
void itemSelected( QListViewItem* );
void selectionChanged();
void toggleFirstName();
void toggleLastName();
void toggleAddress();
void toggleEMail();
void findEntries();
protected:
void setupTabWidget();
void setupListView();
QGridLayout *mainGrid;
QTabWidget *tabWidget;
QListView *listView;
QPushButton *add, *change, *find;
QLineEdit *iFirstName, *iLastName, *iAddress, *iEMail,
*sFirstName, *sLastName, *sAddress, *sEMail;
QCheckBox *cFirstName, *cLastName, *cAddress, *cEMail;
};
#endif
centralwidget.cpp
#include "centralwidget.h"
#include <qtabwidget.h>
#include <qlistview.h>
#include <qlayout.h>
#include <qwidget.h>
#include <qlabel.h>
#include <qpushbutton.h>
#include <qlineedit.h>
#include <qlabel.h>
#include <qcheckbox.h>
#include <qfile.h>
#include <qtextstream.h>
ABCentralWidget::ABCentralWidget( QWidget *parent, const char *name )
: QWidget( parent, name )
{
mainGrid = new QGridLayout( this, 2, 1, 5, 5 );
setupTabWidget();
setupListView();
mainGrid->setRowStretch( 0, 0 );
mainGrid->setRowStretch( 1, 1 );
}
void ABCentralWidget::setupListView()
{
listView = new QListView( this );
listView->addColumn( "First Name" );
listView->addColumn( "Last Name" );
listView->addColumn( "Address" );
listView->addColumn( "E-Mail" );
listView->setSelectionMode( QListView::Single );
connect( listView, SIGNAL( clicked( QListViewItem* ) ), this, SLOT( itemSelected( QListViewItem* ) ) );
mainGrid->addWidget( listView, 1, 0 );
listView->setAllColumnsShowFocus( TRUE );
}
void ABCentralWidget::save( const QString &filename )
{
if ( !listView->firstChild() )
return;
QFile f( filename );
if ( !f.open( IO_WriteOnly ) )
return;
QTextStream t( &f );
QListViewItemIterator it( listView );
for ( ; it.current(); ++it )
for ( unsigned int i = 0; i < 4; i++ )
t << it.current()->text( i ) << "n";
f.close();
}
void ABCentralWidget::load( const QString &filename )
{
listView->clear();
QFile f( filename );
if ( !f.open( IO_ReadOnly ) )
return;
QTextStream t( &f );
while ( !t.atEnd() ) {
QListViewItem *item = new QListViewItem( listView );
for ( unsigned int i = 0; i < 4; i++ )
item->setText( i, t.readLine() );
}
f.close();
}
void ABCentralWidget::addEntry()
{
if ( !iFirstName->text().isEmpty() || !iLastName->text().isEmpty() ||
!iAddress->text().isEmpty() || !iEMail->text().isEmpty() ) {
QListViewItem *item = new QListViewItem( listView );
item->setText( 0, iFirstName->text() );
item->setText( 1, iLastName->text() );
item->setText( 2, iAddress->text() );
item->setText( 3, iEMail->text() );
}
iFirstName->setText( "" );
iLastName->setText( "" );
iAddress->setText( "" );
iEMail->setText( "" );
}
void ABCentralWidget::changeEntry()
{
QListViewItem *item = listView->currentItem();
if ( item &&
( !iFirstName->text().isEmpty() || !iLastName->text().isEmpty() ||
!iAddress->text().isEmpty() || !iEMail->text().isEmpty() ) ) {
item->setText( 0, iFirstName->text() );
item->setText( 1, iLastName->text() );
item->setText( 2, iAddress->text() );
item->setText( 3, iEMail->text() );
}
}
void ABCentralWidget::selectionChanged()
{
iFirstName->setText( "" );
iLastName->setText( "" );
iAddress->setText( "" );
iEMail->setText( "" );
}
void ABCentralWidget::itemSelected( QListViewItem *item )
{
if ( !item )
return;
item->setSelected( TRUE );
item->repaint();
iFirstName->setText( item->text( 0 ) );
iLastName->setText( item->text( 1 ) );
iAddress->setText( item->text( 2 ) );
iEMail->setText( item->text( 3 ) );
}
void ABCentralWidget::toggleFirstName()
{
sFirstName->setText( "" );
if ( cFirstName->isChecked() ) {
sFirstName->setEnabled( TRUE );
sFirstName->setFocus();
}
else
sFirstName->setEnabled( FALSE );
}
void ABCentralWidget::toggleLastName()
{
sLastName->setText( "" );
if ( cLastName->isChecked() ) {
sLastName->setEnabled( TRUE );
sLastName->setFocus();
}
else
sLastName->setEnabled( FALSE );
}
void ABCentralWidget::toggleAddress()
{
sAddress->setText( "" );
if ( cAddress->isChecked() ) {
sAddress->setEnabled( TRUE );
sAddress->setFocus();
}
else
sAddress->setEnabled( FALSE );
}
void ABCentralWidget::toggleEMail()
{
sEMail->setText( "" );
if ( cEMail->isChecked() ) {
sEMail->setEnabled( TRUE );
sEMail->setFocus();
}
else
sEMail->setEnabled( FALSE );
}
void ABCentralWidget::findEntries()
{
if ( !cFirstName->isChecked() &&
!cLastName->isChecked() &&
!cAddress->isChecked() &&
!cEMail->isChecked() ) {
listView->clearSelection();
return;
}
QListViewItemIterator it( listView );
for ( ; it.current(); ++it ) {
bool select = TRUE;
if ( cFirstName->isChecked() ) {
if ( select && it.current()->text( 0 ).contains( sFirstName->text() ) )
select = TRUE;
else
select = FALSE;
}
if ( cLastName->isChecked() ) {
if ( select && it.current()->text( 1 ).contains( sLastName->text() ) )
select = TRUE;
else
select = FALSE;
}
if ( cAddress->isChecked() ) {
if ( select && it.current()->text( 2 ).contains( sAddress->text() ) )
select = TRUE;
else
select = FALSE;
}
if ( cEMail->isChecked() ) {
if ( select && it.current()->text( 3 ).contains( sEMail->text() ) )
select = TRUE;
else
select = FALSE;
}
if ( select )
it.current()->setSelected( TRUE );
else
it.current()->setSelected( FALSE );
it.current()->repaint();
}
}
main.cpp
#include <qapplication.h>
#include "mainwindow.h"
int main( int argc, char ** argv )
{
QApplication a( argc, argv );
ABMainWindow *mw = new ABMainWindow();
mw->setCaption( "Qt Example - Addressbook" );
a.setMainWidget( mw );
mw->show();
a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
int result = a.exec();
delete mw;
return result;
}
|