qtmobility/examples/declarative-sfw-notes/declarative-sfw-notes.qml
changeset 5 453da2cfceef
child 11 06b8e2af4411
equal deleted inserted replaced
4:90517678cc4f 5:453da2cfceef
       
     1 import Qt 4.7
       
     2 // ![0]
       
     3 import QtMobility.serviceframework 1.0
       
     4 // ![0]
       
     5 import "content"
       
     6 
       
     7 Rectangle {
       
     8     property int size: 0
       
     9     property int curr: 0
       
    10     property string searchText: ""
       
    11     property bool validService: false
       
    12 
       
    13     // ![1]
       
    14     property var notesManager: notesService.serviceObject
       
    15     // ![1]
       
    16 
       
    17     id: mainWindow
       
    18     color: "lightgray"
       
    19     width: 220; height: 265
       
    20 
       
    21     SystemPalette { id: activePalette }
       
    22 
       
    23     Rectangle {
       
    24         id: datetimeArea
       
    25         width: 160; height: 20
       
    26         x: 30; y: 120
       
    27         color: "#FFFF7F"
       
    28     }
       
    29 
       
    30     Rectangle {
       
    31         id: noteArea
       
    32         width: 160; height: 110
       
    33         x: 30; y: 140
       
    34         color: "#FFFF7F"
       
    35     }
       
    36 
       
    37     Text {
       
    38         id: title
       
    39         text: "ToDoTool"
       
    40         font.pointSize: 24; font.family: "Nimbus Roman No9 L"; font.bold: true; font.italic:true 
       
    41         color: "blue"
       
    42         y: 5; anchors.horizontalCenter: mainWindow.horizontalCenter
       
    43     }
       
    44 
       
    45     Text {
       
    46         id: countLabel
       
    47         text: curr + "/" + size
       
    48         font.pointSize:10
       
    49         y: 90
       
    50         anchors.horizontalCenter: mainWindow.horizontalCenter
       
    51     }
       
    52 
       
    53     Text {
       
    54         id: datetimeLabel
       
    55         text: ""
       
    56         font.pointSize:10
       
    57         x: 30; y: 120
       
    58         anchors.right: datetimeArea.right
       
    59     }
       
    60 
       
    61     Text {
       
    62         id: noteLabel
       
    63         text: ""
       
    64         font.pointSize: 18; font.family: "Comic Sans MS"; font.italic:true
       
    65         horizontalAlignment: Text.AlignHCenter
       
    66         wrapMode: Text.WordWrap
       
    67         width: noteArea.width
       
    68         anchors.verticalCenter: noteArea.verticalCenter
       
    69         anchors.horizontalCenter: title.horizontalCenter
       
    70     }
       
    71 
       
    72     Button {
       
    73         id: addButton
       
    74         image: "icons/addIcon.png"
       
    75         width: 60; height: 30
       
    76         x: 20; y: 40
       
    77 
       
    78         onClicked: {
       
    79             if (validService) { addDialog.opacity = 1; }
       
    80         }
       
    81     }
       
    82 
       
    83     Button {
       
    84         id: deleteButton
       
    85         image: "icons/deleteIcon.png"
       
    86         width: 60; height: 30
       
    87         x: 80; y: 40
       
    88 
       
    89         onClicked: {
       
    90             if (validService && size > 0) { deleteDialog.opacity = 1; }
       
    91         }
       
    92     }
       
    93 
       
    94     Button {
       
    95         id: searchButton
       
    96         image: "icons/searchIcon.png"
       
    97         width: 60; height: 30
       
    98         x: 140; y: 40
       
    99 
       
   100         onClicked: {
       
   101             if (validService) { searchDialog.opacity = 1; }
       
   102         }
       
   103     }
       
   104 
       
   105     Button {
       
   106         id: nextButton
       
   107         image: "icons/nextIcon.png"
       
   108         width: 40; height: 30
       
   109         x: 130; y: 80
       
   110 
       
   111         onClicked: { 
       
   112             if (validService && curr < size) { 
       
   113                 curr++;
       
   114                 refreshNotes();
       
   115             }
       
   116         }
       
   117     }
       
   118 
       
   119     Button {
       
   120         id: prevButton
       
   121         image: "icons/prevIcon.png"
       
   122         width: 40; height: 30
       
   123         x: 50; y: 80
       
   124 
       
   125         onClicked: {
       
   126             if (validService && curr > 1) {
       
   127                 curr--;
       
   128                 refreshNotes();
       
   129             }
       
   130         }
       
   131     }
       
   132 
       
   133     InputDialog {
       
   134         id: interfaceDialog
       
   135         text: "Specify Notes Manager Interface"
       
   136         defaultText: "com.nokia.qt.examples.NotesManager"
       
   137         cancelable: false
       
   138         opacity: 1
       
   139 
       
   140         onConfirmed: {
       
   141             interfaceDialog.defaultText = input;
       
   142             
       
   143             // ![2]
       
   144             notesService.interfaceName = input;
       
   145 
       
   146             if (notesService.valid) {
       
   147                 notesManager = notesService.serviceObject;
       
   148             // ![2]
       
   149                 var list = notesManager.noteSet;
       
   150                 if (list.length > 0) {curr = 1;}
       
   151 
       
   152                 refreshNotes();
       
   153                 
       
   154                 validService = true;
       
   155             } else {
       
   156                 invalidDialog.opacity = 1;
       
   157                 noteLabel.text = "Notes Manager service not found"
       
   158             }
       
   159         }
       
   160     }
       
   161 
       
   162     InputDialog {
       
   163         id: addDialog
       
   164         text: "Add a new note + alarm of format:\nnote#yyyy-mm-dd#hh:mm"
       
   165 
       
   166         onConfirmed: {
       
   167             var note = input.split("#");
       
   168 
       
   169             if (note.length == 3) {
       
   170                 var date = note[1].split("-");
       
   171                 var time = note[2].split(":");
       
   172 
       
   173                 if (date.length == 3 && time.length ==2) {
       
   174                     notesManager.addNote(note[0], note[1] + " " + note[2] + ":00");
       
   175                 }
       
   176             } else {
       
   177                     notesManager.addNote(note[0], currentDateTime() + ":00");
       
   178             }
       
   179 
       
   180             refreshNotes();
       
   181         }
       
   182     }
       
   183 
       
   184     InputDialog {
       
   185         id: searchDialog
       
   186         text: "Find a note:"
       
   187         size: 100
       
   188 
       
   189         onConfirmed: {
       
   190             searchText = input;
       
   191             curr = 1; 
       
   192             refreshNotes()
       
   193         }
       
   194     }
       
   195 
       
   196     Connections {
       
   197         target: notesManager
       
   198         
       
   199         onSoundAlarm: {
       
   200             alarmDialog.text = "ALERT SOUNDED!!!" + "\n\n" + 
       
   201                                formatDateTime(alarm) + "\n\n" + notesManager.alarmMessage;
       
   202             alarmDialog.opacity = 1;
       
   203         }
       
   204     }
       
   205 
       
   206     Dialog {
       
   207         id: deleteDialog
       
   208         text: "Confirm removing this note item?"
       
   209 
       
   210         onConfirmed: {
       
   211             // ![3]
       
   212             var list = notesManager.noteSet;
       
   213             notesManager.removeNote(list[curr-1].index);
       
   214             // ![3]
       
   215 
       
   216             if (curr > 1) { curr--; }
       
   217 
       
   218             refreshNotes();
       
   219         }
       
   220     }
       
   221 
       
   222     Dialog {
       
   223         id: alarmDialog
       
   224         text: "ALERT SOUNDED!!!"
       
   225         cancelable: false
       
   226     }
       
   227 
       
   228     Dialog {
       
   229         id: invalidDialog
       
   230         text: "No valid default interface for:\n\n\"" + interfaceDialog.defaultText + "\""
       
   231         cancelable: false
       
   232     }
       
   233 
       
   234     function refreshNotes()
       
   235     {
       
   236         // ![4]
       
   237         notesManager.setSearch(searchText);
       
   238         var list = notesManager.noteSet;
       
   239         size = list.length;
       
   240         
       
   241         // ![4]
       
   242        
       
   243         if (size < 1) curr = 0;
       
   244         else if (size > 0 && curr == 0) curr = 1;
       
   245       
       
   246         // ![5]
       
   247         if (size > 0) {
       
   248             noteLabel.text = list[curr-1].message;
       
   249             datetimeLabel.text = formatDateTime(list[curr-1].alarm);
       
   250         }
       
   251         // ![5]
       
   252         else {
       
   253             noteLabel.text = "Click + to add a new note";
       
   254             datetimeLabel.text = "";
       
   255         }
       
   256     }
       
   257 
       
   258     function formatDateTime(datetime)
       
   259     {
       
   260         var dt = new Date(datetime);
       
   261 
       
   262         var month = (dt.getMonth() + 1) + ""; 
       
   263         if (month.length == 1)  month = "0" + month;
       
   264 
       
   265         var date = dt.getDate() + ""; 
       
   266         if (date.length == 1)  date= "0" + date;
       
   267             
       
   268         var hour = dt.getHours() + ""; 
       
   269         if (hour.length == 1)  hour = "0" + hour;
       
   270             
       
   271         var mins = dt.getMinutes() + ""; 
       
   272         if (mins.length == 1)  mins = "0" + mins;
       
   273 
       
   274         return (dt.getFullYear() + "-" + month + "-" + date + " " + hour + ":" + mins);
       
   275     }
       
   276 
       
   277     function currentDateTime()
       
   278     {
       
   279         var dt = new Date();
       
   280 
       
   281         return formatDateTime(dt);
       
   282     }
       
   283 
       
   284     // ![6]
       
   285     Service {
       
   286         id: notesService
       
   287         interfaceName: "com.nokia.qt.examples.NotesManager"
       
   288     }
       
   289     // ![6]
       
   290 }