File indexing completed on 2024-05-12 04:57:48

0001 #ifndef LINEEDIT_H
0002 #define LINEEDIT_H
0003 
0004 /**
0005 * Copyright (c) 2008 - 2009, Benjamin C. Meyer <ben@meyerhome.net>
0006 *
0007 * Redistribution and use in source and binary forms, with or without
0008 * modification, are permitted provided that the following conditions
0009 * are met:
0010 * 1. Redistributions of source code must retain the above copyright
0011 * notice, this list of conditions and the following disclaimer.
0012 * 2. Redistributions in binary form must reproduce the above copyright
0013 * notice, this list of conditions and the following disclaimer in the
0014 * documentation and/or other materials provided with the distribution.
0015 * 3. Neither the name of the Benjamin Meyer nor the names of its contributors
0016 * may be used to endorse or promote products derived from this software
0017 * without specific prior written permission.
0018 *
0019 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
0020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0022 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
0023 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
0024 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
0025 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
0026 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
0027 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
0028 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0029 * SUCH DAMAGE.
0030 */
0031 /* ============================================================
0032 * Falkon - Qt web browser
0033 * Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
0034 *
0035 * This program is free software: you can redistribute it and/or modify
0036 * it under the terms of the GNU General Public License as published by
0037 * the Free Software Foundation, either version 3 of the License, or
0038 * (at your option) any later version.
0039 *
0040 * This program is distributed in the hope that it will be useful,
0041 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0042 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0043 * GNU General Public License for more details.
0044 *
0045 * You should have received a copy of the GNU General Public License
0046 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0047 * ============================================================ */
0048 
0049 #include <QLineEdit>
0050 #include <QTextLayout>
0051 #include "qzcommon.h"
0052 
0053 class QHBoxLayout;
0054 
0055 /*
0056 LineEdit is a subclass of QLineEdit that provides an easy and simple
0057 way to add widgets on the left or right hand side of the text.
0058 
0059 The layout of the widgets on either side are handled by a QHBoxLayout.
0060 You can set the spacing around the widgets with setWidgetSpacing().
0061 
0062 As widgets are added to the class they are inserted from the outside
0063 into the center of the widget.
0064 */
0065 class SideWidget;
0066 class FALKON_EXPORT LineEdit : public QLineEdit
0067 {
0068     Q_OBJECT
0069     Q_PROPERTY(QSize fixedsize READ size WRITE setFixedSize)
0070     Q_PROPERTY(int leftMargin READ leftMargin WRITE setLeftMargin)
0071     Q_PROPERTY(int fixedwidth READ width WRITE setFixedWidth)
0072     Q_PROPERTY(int fixedheight READ height WRITE setFixedHeight)
0073     Q_PROPERTY(int minHeight READ minHeight WRITE setMinHeight)
0074 
0075 public:
0076     using TextFormat = QList<QTextLayout::FormatRange>;
0077 
0078     enum WidgetPosition {
0079         LeftSide,
0080         RightSide
0081     };
0082 
0083     enum EditAction {
0084         Undo = 0,
0085         Redo = 1,
0086         Cut = 2,
0087         Copy = 3,
0088         Paste = 4,
0089         PasteAndGo = 5,
0090         Delete = 6,
0091         ClearAll = 7,
0092         SelectAll = 8
0093     };
0094 
0095     LineEdit(QWidget* parent = nullptr);
0096 
0097     void addWidget(QWidget* widget, WidgetPosition position);
0098     void removeWidget(QWidget* widget);
0099     void setWidgetSpacing(int spacing);
0100     int widgetSpacing() const;
0101     int leftMargin() const;
0102 
0103     void setTextFormat(const TextFormat &format);
0104     void clearTextFormat();
0105 
0106     int minHeight() const;
0107     void setMinHeight(int height);
0108 
0109     QSize sizeHint() const override;
0110     QAction* editAction(EditAction action) const;
0111 
0112 public Q_SLOTS:
0113     void setLeftMargin(int margin);
0114     void updateTextMargins();
0115 
0116 protected:
0117     void focusInEvent(QFocusEvent* event) override;
0118     void mousePressEvent(QMouseEvent* event) override;
0119     void mouseReleaseEvent(QMouseEvent* event) override;
0120     void mouseDoubleClickEvent(QMouseEvent* event) override;
0121     void resizeEvent(QResizeEvent *event) override;
0122     bool event(QEvent* event) override;
0123 
0124     QMenu* createContextMenu();
0125 
0126 private Q_SLOTS:
0127     void updateActions();
0128     void updatePasteActions();
0129     void slotDelete();
0130 
0131 private:
0132     void init();
0133 
0134     SideWidget* m_leftWidget;
0135     SideWidget* m_rightWidget;
0136     QHBoxLayout* m_leftLayout;
0137     QHBoxLayout* m_rightLayout;
0138     QHBoxLayout* mainLayout;
0139     QAction* m_editActions[9];
0140 
0141     int m_minHeight;
0142     int m_leftMargin;
0143     bool m_ignoreMousePress;
0144 };
0145 
0146 
0147 class FALKON_EXPORT SideWidget : public QWidget
0148 {
0149     Q_OBJECT
0150 
0151 Q_SIGNALS:
0152     void sizeHintChanged();
0153 
0154 public:
0155     SideWidget(QWidget* parent = nullptr);
0156 
0157 protected:
0158     bool event(QEvent* event) override;
0159 
0160 };
0161 
0162 #endif // LINEEDIT_H