Changeset 72153 in vbox
- Timestamp:
- May 8, 2018 7:00:29 AM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Frontends/VirtualBox/src/logviewer/UIVMLogViewerFilterPanel.cpp
r71638 r72153 47 47 #endif /* !VBOX_WITH_PRECOMPILED_HEADERS */ 48 48 49 /* UIVMFilterLineEdit class is used to display and modify the list of filter terms. 50 the terms are displayed as words with spaces in between and it is possible to 51 remove these terms one by one by selecting them or completely by the clearAll button 52 located on the right side of the line edit: */ 49 50 /********************************************************************************************************************************* 51 * UIVMFilterLineEdit definition. * 52 *********************************************************************************************************************************/ 53 54 /** UIVMFilterLineEdit class is used to display and modify the list of filter terms. 55 * the terms are displayed as words with spaces in between and it is possible to 56 * remove these terms one by one by selecting them or completely by the clearAll button 57 * located on the right side of the line edit: */ 53 58 class UIVMFilterLineEdit : public QLineEdit 54 59 { … … 62 67 public: 63 68 64 UIVMFilterLineEdit(QWidget *parent = 0) 65 :QLineEdit(parent) 66 , m_pRemoveTermButton(0) 67 , m_iRemoveTermButtonSize(16) 68 , m_iTrailingSpaceCount(1) 69 { 70 setReadOnly(true); 71 home(false); 72 createButtons(); 73 /** Try to guess the width of the space between filter terms so that remove button 74 we display when a term is selected does not hide the next/previous word: */ 75 int spaceWidth = fontMetrics().width(' '); 76 if (spaceWidth != 0) 77 m_iTrailingSpaceCount = (m_iRemoveTermButtonSize / spaceWidth) + 1; 78 } 79 80 void addFilterTerm(const QString& filterTermString) 81 { 82 if (text().isEmpty()) 83 insert(filterTermString); 84 else 85 { 86 QString newString(filterTermString); 87 QString space(m_iTrailingSpaceCount, QChar(' ')); 88 insert(newString.prepend(space)); 89 } 90 } 91 92 void clearAll() 93 { 94 if (text().isEmpty()) 95 return; 96 sltClearAll(); 97 } 69 UIVMFilterLineEdit(QWidget *parent = 0); 70 void addFilterTerm(const QString& filterTermString); 71 void clearAll(); 98 72 99 73 protected: 100 74 101 /* Overload the mouseXXXEvent to control how selection is done: */ 102 virtual void mouseDoubleClickEvent(QMouseEvent *){} 103 virtual void mouseMoveEvent(QMouseEvent *){} 104 virtual void mousePressEvent(QMouseEvent * event) 105 { 106 /* Simulate double mouse click to select a word with a single click: */ 107 QLineEdit::mouseDoubleClickEvent(event); 108 } 109 75 /* Delete mouseDoubleClick and mouseMoveEvent implementations of the base class */ 76 virtual void mouseDoubleClickEvent(QMouseEvent *) /* override */{} 77 virtual void mouseMoveEvent(QMouseEvent *) /* override */{} 78 /* Override the mousePressEvent to control how selection is done: */ 79 virtual void mousePressEvent(QMouseEvent * event) /* override */; 110 80 virtual void mouseReleaseEvent(QMouseEvent *){} 111 virtual void paintEvent(QPaintEvent *event) 112 { 113 QLineEdit::paintEvent(event); 114 int clearButtonSize = height(); 115 m_pClearAllButton->setGeometry(width() - clearButtonSize, 0, clearButtonSize, clearButtonSize); 116 /* If we have a selected term move the m_pRemoveTermButton to the end of the 117 or start of the word (depending on the location of the word within line edit itself: */ 118 if (hasSelectedText()) 119 { 120 m_pRemoveTermButton->show(); 121 int buttonY = 0.5 * (height() - 16); 122 int buttonSize = m_iRemoveTermButtonSize; 123 int charWidth = fontMetrics().width('x'); 124 int buttonLeft = cursorRect().right() - 0.5 * charWidth; 125 /* If buttonLeft is in far left of the line edit, move the 126 button to left side of the selected word: */ 127 if (buttonLeft + buttonSize >= width() - clearButtonSize) 128 { 129 int selectionWidth = charWidth * selectedText().length(); 130 buttonLeft -= (selectionWidth + buttonSize); 131 } 132 m_pRemoveTermButton->setGeometry(buttonLeft, buttonY, buttonSize, buttonSize); 133 } 134 else 135 m_pRemoveTermButton->hide(); 136 } 81 virtual void paintEvent(QPaintEvent *event); 137 82 138 83 private slots: 139 84 140 85 /* Nofifies the listeners that selected word (filter term) has been removed: */ 141 void sltRemoveFilterTerm() 142 { 143 if (!hasSelectedText()) 144 return; 145 emit sigFilterTermRemoved(selectedText()); 146 /* Remove the string from text() including the trailing space: */ 147 setText(text().remove(selectionStart(), selectedText().length() + m_iTrailingSpaceCount)); 148 } 149 86 void sltRemoveFilterTerm(); 150 87 /* The whole content is removed. Listeners are notified: */ 151 void sltClearAll() 152 { 153 /* Check if we have some text to avoid recursive calls: */ 154 if (text().isEmpty()) 155 return; 156 157 clear(); 158 emit sigClearAll(); 159 } 88 void sltClearAll(); 160 89 161 90 private: 162 91 163 void createButtons() 164 { 165 m_pRemoveTermButton = new QToolButton(this); 166 if (m_pRemoveTermButton) 167 { 168 m_pRemoveTermButton->setIcon(m_pRemoveTermButton->style()->standardIcon(QStyle::SP_TitleBarCloseButton)); 169 m_pRemoveTermButton->hide(); 170 connect(m_pRemoveTermButton, &QToolButton::clicked, this, &UIVMFilterLineEdit::sltRemoveFilterTerm); 171 } 172 173 m_pClearAllButton = new QToolButton(this); 174 if (m_pClearAllButton) 175 { 176 m_pClearAllButton->setIcon(m_pRemoveTermButton->style()->standardIcon(QStyle::SP_LineEditClearButton)); 177 connect(m_pClearAllButton, &QToolButton::clicked, this, &UIVMFilterLineEdit::sltClearAll); 178 } 179 } 180 92 void createButtons(); 181 93 QToolButton *m_pRemoveTermButton; 182 94 QToolButton *m_pClearAllButton; … … 184 96 int m_iTrailingSpaceCount; 185 97 }; 98 99 100 /********************************************************************************************************************************* 101 * UIVMFilterLineEdit implementation. * 102 *********************************************************************************************************************************/ 103 104 UIVMFilterLineEdit::UIVMFilterLineEdit(QWidget *parent /*= 0*/) 105 :QLineEdit(parent) 106 , m_pRemoveTermButton(0) 107 , m_iRemoveTermButtonSize(16) 108 , m_iTrailingSpaceCount(1) 109 { 110 setReadOnly(true); 111 home(false); 112 createButtons(); 113 /** Try to guess the width of the space between filter terms so that remove button 114 we display when a term is selected does not hide the next/previous word: */ 115 int spaceWidth = fontMetrics().width(' '); 116 if (spaceWidth != 0) 117 m_iTrailingSpaceCount = (m_iRemoveTermButtonSize / spaceWidth) + 1; 118 } 119 120 void UIVMFilterLineEdit::addFilterTerm(const QString& filterTermString) 121 { 122 if (text().isEmpty()) 123 insert(filterTermString); 124 else 125 { 126 QString newString(filterTermString); 127 QString space(m_iTrailingSpaceCount, QChar(' ')); 128 insert(newString.prepend(space)); 129 } 130 } 131 132 void UIVMFilterLineEdit::clearAll() 133 { 134 if (text().isEmpty()) 135 return; 136 sltClearAll(); 137 } 138 139 void UIVMFilterLineEdit::mousePressEvent(QMouseEvent * event) 140 { 141 /* Simulate double mouse click to select a word with a single click: */ 142 QLineEdit::mouseDoubleClickEvent(event); 143 } 144 145 void UIVMFilterLineEdit::paintEvent(QPaintEvent *event) 146 { 147 QLineEdit::paintEvent(event); 148 int clearButtonSize = height(); 149 m_pClearAllButton->setGeometry(width() - clearButtonSize, 0, clearButtonSize, clearButtonSize); 150 /* If we have a selected term move the m_pRemoveTermButton to the end of the 151 or start of the word (depending on the location of the word within line edit itself: */ 152 if (hasSelectedText()) 153 { 154 m_pRemoveTermButton->show(); 155 int buttonY = 0.5 * (height() - 16); 156 int buttonSize = m_iRemoveTermButtonSize; 157 int charWidth = fontMetrics().width('x'); 158 int buttonLeft = cursorRect().right() - 0.5 * charWidth; 159 /* If buttonLeft is in far left of the line edit, move the 160 button to left side of the selected word: */ 161 if (buttonLeft + buttonSize >= width() - clearButtonSize) 162 { 163 int selectionWidth = charWidth * selectedText().length(); 164 buttonLeft -= (selectionWidth + buttonSize); 165 } 166 m_pRemoveTermButton->setGeometry(buttonLeft, buttonY, buttonSize, buttonSize); 167 } 168 else 169 m_pRemoveTermButton->hide(); 170 } 171 172 void UIVMFilterLineEdit::sltRemoveFilterTerm() 173 { 174 if (!hasSelectedText()) 175 return; 176 emit sigFilterTermRemoved(selectedText()); 177 /* Remove the string from text() including the trailing space: */ 178 setText(text().remove(selectionStart(), selectedText().length() + m_iTrailingSpaceCount)); 179 } 180 181 void UIVMFilterLineEdit::sltClearAll() 182 { 183 /* Check if we have some text to avoid recursive calls: */ 184 if (text().isEmpty()) 185 return; 186 187 clear(); 188 emit sigClearAll(); 189 } 190 191 void UIVMFilterLineEdit::createButtons() 192 { 193 m_pRemoveTermButton = new QToolButton(this); 194 if (m_pRemoveTermButton) 195 { 196 m_pRemoveTermButton->setIcon(m_pRemoveTermButton->style()->standardIcon(QStyle::SP_TitleBarCloseButton)); 197 m_pRemoveTermButton->hide(); 198 connect(m_pRemoveTermButton, &QToolButton::clicked, this, &UIVMFilterLineEdit::sltRemoveFilterTerm); 199 } 200 201 m_pClearAllButton = new QToolButton(this); 202 if (m_pClearAllButton) 203 { 204 m_pClearAllButton->setIcon(m_pRemoveTermButton->style()->standardIcon(QStyle::SP_LineEditClearButton)); 205 connect(m_pClearAllButton, &QToolButton::clicked, this, &UIVMFilterLineEdit::sltClearAll); 206 } 207 } 186 208 187 209 UIVMLogViewerFilterPanel::UIVMLogViewerFilterPanel(QWidget *pParent, UIVMLogViewerWidget *pViewer) … … 541 563 542 564 #include "UIVMLogViewerFilterPanel.moc" 543
Note:
See TracChangeset
for help on using the changeset viewer.