OSDN Git Service

Add generic icons and remember path selection.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / dialog / SetIcon.java
1 /*
2  * This file is part of NeverNote 
3  * Copyright 2009 Randy Baumgarte
4  * 
5  * This file may be licensed under the terms of of the
6  * GNU General Public License Version 2 (the ``GPL'').
7  *
8  * Software distributed under the License is distributed
9  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
10  * express or implied. See the GPL for the specific language
11  * governing rights and limitations.
12  *
13  * You should have received a copy of the GPL along with this
14  * program. If not, go to http://www.gnu.org/licenses/gpl.html
15  * or write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18 */
19
20 package cx.fbn.nevernote.dialog;
21
22 import com.trolltech.qt.gui.QCheckBox;
23 import com.trolltech.qt.gui.QDialog;
24 import com.trolltech.qt.gui.QFileDialog;
25 import com.trolltech.qt.gui.QFileDialog.AcceptMode;
26 import com.trolltech.qt.gui.QFileDialog.FileMode;
27 import com.trolltech.qt.gui.QGridLayout;
28 import com.trolltech.qt.gui.QIcon;
29 import com.trolltech.qt.gui.QPushButton;
30 import com.trolltech.qt.gui.QSizePolicy.Policy;
31
32 import cx.fbn.nevernote.Global;
33
34 public class SetIcon extends QDialog {
35         private boolean okPressed;
36         QPushButton     ok;
37         QPushButton             iconButton;
38         QCheckBox               useDefault;
39         QIcon                   defaultIcon;
40         boolean                 startUseDefault;
41         String                  path;
42         
43         private final String iconPath = new String("classpath:cx/fbn/nevernote/icons/");
44         
45         // Constructor
46         public SetIcon(QIcon i, String path) {
47                 okPressed = false;
48                 this.path = path;
49                 setWindowTitle(tr("Set Icon"));
50                 QGridLayout grid = new QGridLayout();
51                 setWindowIcon(new QIcon(iconPath+"nevernote.png"));
52                 setLayout(grid);
53                 
54                 QGridLayout textGrid = new QGridLayout();
55                 textGrid.setContentsMargins(10, 10,-10, -10);
56                 useDefault = new QCheckBox();
57                 iconButton = new QPushButton();
58                 iconButton.setSizePolicy(Policy.Fixed, Policy.Fixed);
59                 iconButton.clicked.connect(this, "iconButtonPressed()");
60                 useDefault.setText(tr("Use Default"));
61                 iconButton.setIcon(i);
62                 
63                 textGrid.addWidget(iconButton,1,1);
64                 textGrid.addWidget(useDefault,2,1);
65                 useDefault.clicked.connect(this, "useDefaultIconChecked(Boolean)");
66                 grid.addLayout(textGrid,1,1);
67                 
68                 QGridLayout buttonGrid = new QGridLayout();
69                 ok = new QPushButton(tr("OK"));
70                 ok.clicked.connect(this, "okButtonPressed()");
71                 ok.setEnabled(false);
72                 QPushButton cancel = new QPushButton(tr("Cancel"));
73                 cancel.clicked.connect(this, "cancelButtonPressed()");
74                 buttonGrid.addWidget(ok, 3, 1);
75                 buttonGrid.addWidget(cancel, 3,2);
76                 grid.addLayout(buttonGrid,2,1);
77         }
78         
79         // The OK button was pressed
80         @SuppressWarnings("unused")
81         private void okButtonPressed() {
82                 okPressed = true;
83                 close();
84         }
85         
86         // The CANCEL button was pressed
87         @SuppressWarnings("unused")
88         private void cancelButtonPressed() {
89                 okPressed = false;
90                 close();
91         }
92         
93         // Check if the OK button was pressed
94         public boolean okPressed() {
95                 return okPressed;
96         }
97         
98         // Icon Button pressed
99         @SuppressWarnings("unused")
100         private void iconButtonPressed() {
101                 QFileDialog fd = new QFileDialog(this);
102                 fd.setFileMode(FileMode.ExistingFile);
103                 fd.setConfirmOverwrite(true);
104                 fd.setWindowTitle(tr("Icon"));
105                 fd.setFilter(tr("PNG (*.png);;All Files (*.*)"));
106                 fd.setAcceptMode(AcceptMode.AcceptOpen);
107                 if (path == null || path.equals(""))
108                         fd.setDirectory(Global.getFileManager().getImageDirPath(""));
109                 else
110                         fd.setDirectory(path);
111                 if (fd.exec() == 0 || fd.selectedFiles().size() == 0) {
112                         return;
113                 }
114                 
115                 this.path = fd.selectedFiles().get(0);
116                 this.path = path.substring(0,path.lastIndexOf("/"));
117                 ok.setEnabled(true);
118                 String path = fd.selectedFiles().get(0);
119                 iconButton.setIcon(new QIcon(path));
120                 iconButton.setSizePolicy(Policy.Fixed, Policy.Fixed);
121         }
122         
123         public void setUseDefaultIcon(boolean val) {
124                 useDefault.setChecked(val);
125                 iconButton.setEnabled(!val);
126                 startUseDefault = val;
127         }
128         
129         public QIcon getIcon() {
130                 if (useDefault.isChecked())
131                         return null;
132                 return iconButton.icon();
133         }
134         
135         public void useDefaultIconChecked(Boolean value) {
136                 iconButton.setEnabled(!value);
137                 
138                 if (value != startUseDefault) 
139                         ok.setEnabled(true);
140                 else
141                         ok.setEnabled(false);
142         }
143         
144         public String getFileType() {
145                 return "PNG";
146         }
147         
148         public String getPath() {
149                 return path;
150         }
151 }