Überblick
Beispiele
Schnappschüsse
Vergleiche
Anwendungen
Herunterladen
Documentation
Basar
Status & Fahrplan
Häufig gestellte Fragen
Autoren & Lizenz
Forum
Ultimate++ finanziell unterstützen
Diese Seite durchsuchen
Sprache
Deutsch













SourceForge.net Logo



AddressBook

 

Simple address book application

 

 

AddressBook.cpp

 

// This is an upp conversion of qt example, see

// http://doc.trolltech.com/3.0/addressbook-example.html

 

#include <CtrlLib/CtrlLib.h>

#include <Report/Report.h>

 

using namespace Upp;

 

#define LAYOUTFILE <AddressBook/AddressBook.lay>

#include <CtrlCore/lay.h>

 

class AddressBook : public WithAddressBookLayout<TopWindow> {

    WithModifyLayout<ParentCtrl> modify;

    WithSearchLayout<ParentCtrl> 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 [* " + DeQtf(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; q++)

            out.PutLine(String(array.Get(i, q)));

}

 

void AddressBook::SaveAs()

{

    if(!fs.ExecuteSaveAs()) return;

    filename = fs;

    Save();

}

 

void AddressBook::Print()

{

    String qtf;

    qtf = "{{1:1:1:1 Name:: Surname:: Address:: Email";

    for(int i = 0; i < array.GetCount(); i++)

        for(int q = 0; q < 4; q++)

            qtf << ":: " << DeQtf((String)array.Get(i, q));

    Report report;

    report << qtf;

    Perform(report);

}

 

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);

}

 

 

 

AddressBook.lay

 

LAYOUT(AddressBookLayout, 512, 456)

    ITEM(MenuBar, menu, LeftPosZ(0, 216).TopPosZ(0, 20))

    ITEM(TabCtrl, tab, LeftPosZ(8, 496).TopPosZ(32, 84))

    ITEM(ArrayCtrl, array, LeftPosZ(8, 496).TopPosZ(124, 324))

END_LAYOUT

 

LAYOUT(ModifyLayout, 480, 52)

    ITEM(Label, dv___0, SetLabel(t_("&Name")).LeftPosZ(8, 48).TopPosZ(8, 15))

    ITEM(EditField, name, LeftPosZ(8, 96).TopPosZ(28, 19))

    ITEM(Label, dv___2, SetLabel(t_("&Surname")).LeftPosZ(108, 48).TopPosZ(8, 15))

    ITEM(EditField, surname, LeftPosZ(108, 96).TopPosZ(28, 19))

    ITEM(Label, dv___4, SetLabel(t_("&Address")).LeftPosZ(208, 48).TopPosZ(8, 15))

    ITEM(EditField, address, LeftPosZ(208, 96).TopPosZ(28, 19))

    ITEM(Label, dv___6, SetLabel(t_("&Email")).LeftPosZ(308, 48).TopPosZ(8, 15))

    ITEM(EditField, email, LeftPosZ(308, 96).TopPosZ(28, 19))

    ITEM(Button, add, SetLabel(t_("Ad&d")).LeftPosZ(416, 56).TopPosZ(4, 20))

    ITEM(Button, change, SetLabel(t_("&Change")).LeftPosZ(416, 56).TopPosZ(28, 20))

END_LAYOUT

 

LAYOUT(SearchLayout, 480, 56)

    ITEM(Option, oname, SetLabel(t_("&Name")).LeftPosZ(8, 56).TopPosZ(8, 18))

    ITEM(EditField, name, LeftPosZ(8, 96).TopPosZ(28, 19))

    ITEM(Option, osurname, SetLabel(t_("&Surname")).LeftPosZ(108, 64).TopPosZ(8, 18))

    ITEM(EditField, surname, LeftPosZ(108, 96).TopPosZ(28, 19))

    ITEM(Option, oaddress, SetLabel(t_("&Address")).LeftPosZ(208, 64).TopPosZ(8, 18))

    ITEM(EditField, address, LeftPosZ(208, 96).TopPosZ(28, 19))

    ITEM(Option, oemail, SetLabel(t_("&Email")).LeftPosZ(308, 52).TopPosZ(8, 18))

    ITEM(EditField, email, LeftPosZ(308, 96).TopPosZ(28, 19))

    ITEM(Button, search, SetLabel(t_("&Search")).LeftPosZ(416, 56).TopPosZ(28, 20))

END_LAYOUT

 

 

 

 

 

Diese Seite gibt es auch in english, català, čeština, español, euskara, français, română, русский, 中文(简体) und 中文(繁體). Willst du mitmachen?