rwlbuis | cdba55a | 2006-08-18 22:47:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 Dirk Mueller <mueller@kde.org> |
| 3 | * (C) 2006 Nikolas Zimmermann <zimmermann@kde.org> |
| 4 | * |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
| 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 20 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 21 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 22 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 23 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 24 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <QScrollBar> |
| 30 | #include <QListWidget> |
| 31 | |
rwlbuis | 9829433 | 2006-08-20 22:27:28 +0000 | [diff] [blame] | 32 | #include "Font.h" |
rwlbuis | cdba55a | 2006-08-18 22:47:37 +0000 | [diff] [blame] | 33 | #include "IntSize.h" |
rwlbuis | cdba55a | 2006-08-18 22:47:37 +0000 | [diff] [blame] | 34 | #include "ListBox.h" |
| 35 | |
| 36 | namespace WebCore { |
| 37 | |
| 38 | ListBox::ListBox() |
| 39 | : ScrollView() |
| 40 | , m_listWidget(0) |
| 41 | , m_selectionMode(Single) |
| 42 | , _width(0.0) |
| 43 | , _widthGood(false) |
| 44 | { |
| 45 | qDebug("ListBox::ListBox(), this=%p", this); |
| 46 | } |
| 47 | |
| 48 | ListBox::~ListBox() |
| 49 | { |
| 50 | qDebug("ListBox::~ListBox(), this=%p", this); |
| 51 | } |
| 52 | |
| 53 | void ListBox::setParentWidget(QWidget* parent) |
| 54 | { |
| 55 | qDebug("ListBox::setParentWidget(), parent=%p", parent); |
| 56 | ScrollView::setParentWidget(parent); |
| 57 | |
| 58 | Q_ASSERT(m_listWidget == 0); |
| 59 | m_listWidget = new QListWidget(parent); |
| 60 | |
| 61 | if(m_selectionMode == Single) |
| 62 | m_listWidget->setSelectionMode(QAbstractItemView::SingleSelection); |
| 63 | else |
| 64 | m_listWidget->setSelectionMode(QAbstractItemView::ExtendedSelection); |
| 65 | |
| 66 | setQWidget(m_listWidget); |
| 67 | } |
| 68 | |
| 69 | IntSize ListBox::sizeForNumberOfLines(int numLines) const |
| 70 | { |
| 71 | QSize size(0, 0); |
| 72 | |
| 73 | for (int i = 0; i <= numLines; i++) { |
| 74 | QListWidgetItem *item = m_listWidget->item(i); |
| 75 | Q_ASSERT(item != 0); |
| 76 | |
| 77 | QRect visual = m_listWidget->visualItemRect(item); |
| 78 | |
| 79 | size.setWidth(qMax(size.width(), visual.width())); |
| 80 | size.setHeight(qMax(size.height(), visual.height())); |
| 81 | } |
| 82 | |
| 83 | // Logic taken from our khtml :-) |
| 84 | size.setWidth(size.width() + 2 * m_listWidget->frameWidth() + m_listWidget->verticalScrollBar()->sizeHint().width()); |
| 85 | size.setHeight(numLines * size.height() + 2 * m_listWidget->frameWidth()); |
| 86 | |
| 87 | return size; |
| 88 | } |
| 89 | |
| 90 | void ListBox::setSelectionMode(SelectionMode mode) |
| 91 | { |
| 92 | // We can not directly assign to m_listWidget here, as |
| 93 | // setSelectionMode() is called from DeprecatedRenderSelect before |
| 94 | // setWidget() and our setParentWidget() functions have been called... |
| 95 | m_selectionMode = mode; |
| 96 | } |
| 97 | |
| 98 | void ListBox::clear() |
| 99 | { |
| 100 | m_listWidget->clear(); |
| 101 | } |
| 102 | |
| 103 | void ListBox::doneAppendingItems() |
| 104 | { |
| 105 | // no-op |
| 106 | } |
| 107 | |
| 108 | void ListBox::setSelected(int row, bool selected) |
| 109 | { |
| 110 | QListWidgetItem *item = m_listWidget->item(row); |
| 111 | if (!item) |
| 112 | return; |
| 113 | |
| 114 | m_listWidget->setItemSelected(item, selected); |
| 115 | } |
| 116 | |
| 117 | bool ListBox::isSelected(int row) const |
| 118 | { |
| 119 | QListWidgetItem *item = m_listWidget->item(row); |
| 120 | if (!item) |
| 121 | return false; |
| 122 | |
| 123 | return m_listWidget->isItemSelected(item); |
| 124 | } |
| 125 | |
| 126 | void ListBox::setEnabled(bool value) |
| 127 | { |
| 128 | m_listWidget->setEnabled(value); |
| 129 | } |
| 130 | |
| 131 | bool ListBox::isEnabled() |
| 132 | { |
| 133 | return m_listWidget->isEnabled(); |
| 134 | } |
| 135 | |
| 136 | void ListBox::setWritingDirection(TextDirection dir) |
| 137 | { |
| 138 | Qt::LayoutDirection qDir; |
| 139 | |
| 140 | switch(dir) |
| 141 | { |
| 142 | case LTR: |
| 143 | qDir = Qt::LeftToRight; |
| 144 | break; |
| 145 | case RTL: |
| 146 | qDir = Qt::RightToLeft; |
| 147 | break; |
| 148 | } |
| 149 | |
| 150 | m_listWidget->setLayoutDirection(qDir); |
| 151 | } |
| 152 | |
| 153 | Widget::FocusPolicy ListBox::focusPolicy() const |
| 154 | { |
| 155 | return Widget::focusPolicy(); |
| 156 | } |
| 157 | |
| 158 | bool ListBox::checksDescendantsForFocus() const |
| 159 | { |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | void ListBox::clearCachedTextRenderers() |
| 164 | { |
| 165 | // no-op |
| 166 | } |
| 167 | |
| 168 | void ListBox::setFont(const Font& font) |
| 169 | { |
rwlbuis | 9829433 | 2006-08-20 22:27:28 +0000 | [diff] [blame] | 170 | m_listWidget->setFont(font); |
rwlbuis | cdba55a | 2006-08-18 22:47:37 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void ListBox::appendItem(const DeprecatedString& string, ListBoxItemType type, bool enabled) |
| 174 | { |
| 175 | // FIXME: take into account type/enabled... |
| 176 | Q_ASSERT(m_listWidget != 0); |
rwlbuis | 9829433 | 2006-08-20 22:27:28 +0000 | [diff] [blame] | 177 | (void) new QListWidgetItem(string, m_listWidget); // No this does not leak... |
rwlbuis | cdba55a | 2006-08-18 22:47:37 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | }; |
| 181 | |
| 182 | // vim: ts=4 sw=4 et |