OSDN Git Service

検索ボックスをツールバー右側に移動&改良した
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / gui / SearchEdit.java
1 package cx.fbn.nevernote.gui;
2
3 import com.trolltech.qt.core.QSize;
4 import com.trolltech.qt.gui.QFocusEvent;
5 import com.trolltech.qt.gui.QLineEdit;
6 import com.trolltech.qt.gui.QResizeEvent;
7 import com.trolltech.qt.gui.QStyle.PixelMetric;
8
9 public class SearchEdit extends QLineEdit {
10         private final SearchClearButton clearButton;
11         private final String inactiveColor;
12         private final String activeColor;
13         private final String defaultText;
14
15         
16         public SearchEdit(String iconPath) {
17                 inactiveColor = new String("QLineEdit {color: gray; font:italic;} ");
18                 activeColor = new String("QLineEdit {color: black; font:normal;} ");
19                 
20                 this.clearButton = new SearchClearButton(this, iconPath);
21                 this.clearButton.clicked.connect(this, "clear()");
22                 
23                 this.textChanged.connect(this, "updateClearButton(String)");
24                 int frameWidth = this.style().pixelMetric(PixelMetric.PM_DefaultFrameWidth);
25                 this.setStyleSheet("QLineEdit { padding-right: " + (clearButton.sizeHint().width() + frameWidth + 1) + "px; } ");
26                 defaultText = new String(tr("Search"));
27                 this.setText(defaultText);
28                 this.setStyleSheet(inactiveColor);
29         }
30         
31         @Override
32         protected void resizeEvent(QResizeEvent event) {
33                 QSize sz = clearButton.sizeHint();
34                 int frameWidth = this.style().pixelMetric(PixelMetric.PM_DefaultFrameWidth);
35                 clearButton.move(this.rect().right() - frameWidth - sz.width(), (this.rect().bottom() + 1 - sz.height()) / 2);
36         }
37         
38         @SuppressWarnings("unused")
39         private void updateClearButton(String text) {
40                 clearButton.setVisible(!text.isEmpty());
41         }
42         
43         public void setdefaultText() {
44                 this.setText(defaultText);
45                 this.setStyleSheet(inactiveColor);
46         }
47         
48         @Override
49         protected void focusInEvent(QFocusEvent event) {
50                 super.focusInEvent(event);
51                 if (this.text().equals(defaultText)) {
52                         this.blockSignals(true);
53                         this.setText("");
54                         this.blockSignals(false);
55                 }
56                 this.setStyleSheet(activeColor);
57         }
58         
59         @Override
60         protected void focusOutEvent(QFocusEvent event) {
61                 super.focusOutEvent(event);
62                 if (this.text().trim().equals("")) {
63                         this.blockSignals(true);
64                         this.setText(defaultText);
65                         this.blockSignals(false);
66                         this.setStyleSheet(inactiveColor);
67                 }
68         }
69 }