consoleitem.cpp 4.05 KB
Newer Older
1
/****************************************************************************
Aurindam Jana's avatar
Aurindam Jana committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
Tobias Hunger's avatar
Tobias Hunger committed
4
** Contact: http://www.qt-project.org/legal
Aurindam Jana's avatar
Aurindam Jana committed
5
**
Tobias Hunger's avatar
Tobias Hunger committed
6
** This file is part of Qt Creator.
Aurindam Jana's avatar
Aurindam Jana committed
7
**
Tobias Hunger's avatar
Tobias Hunger committed
8 9 10 11 12 13 14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
Aurindam Jana's avatar
Aurindam Jana committed
15 16
**
** GNU Lesser General Public License Usage
Tobias Hunger's avatar
Tobias Hunger committed
17 18 19 20 21 22 23 24 25
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
Aurindam Jana's avatar
Aurindam Jana committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
Tobias Hunger's avatar
Tobias Hunger committed
28
****************************************************************************/
Aurindam Jana's avatar
Aurindam Jana committed
29

30
#include "consoleitem.h"
31

32
namespace QmlJS {
33 34 35

///////////////////////////////////////////////////////////////////////
//
36
// ConsoleItem
37 38 39
//
///////////////////////////////////////////////////////////////////////

40
ConsoleItem::ConsoleItem(ConsoleItem *parent, ConsoleItem::ItemType itemType,
41 42 43 44 45 46 47 48 49
                               const QString &text)
    : m_parentItem(parent),
      itemType(itemType),
      line(-1)

{
    setText(text);
}

50
ConsoleItem::~ConsoleItem()
51 52 53 54
{
    qDeleteAll(m_childItems);
}

55
ConsoleItem *ConsoleItem::child(int number)
56 57 58 59
{
    return m_childItems.value(number);
}

60
int ConsoleItem::childCount() const
61 62 63 64
{
    return m_childItems.size();
}

65
int ConsoleItem::childNumber() const
66 67
{
    if (m_parentItem)
68
        return m_parentItem->m_childItems.indexOf(const_cast<ConsoleItem *>(this));
69 70 71 72

    return 0;
}

73
bool ConsoleItem::insertChildren(int position, int count)
74 75 76 77 78
{
    if (position < 0 || position > m_childItems.size())
        return false;

    for (int row = 0; row < count; ++row) {
79
        ConsoleItem *item = new ConsoleItem(this, ConsoleItem::UndefinedType,
80 81 82 83 84 85 86
                                                  QString());
        m_childItems.insert(position, item);
    }

    return true;
}

87
void ConsoleItem::insertChild(ConsoleItem *item, bool sorted)
88 89 90 91 92 93 94 95 96 97 98 99 100 101
{
    if (!sorted) {
        m_childItems.insert(m_childItems.count(), item);
        return;
    }

    int i = 0;
    for (; i < m_childItems.count(); i++) {
        if (item->m_text < m_childItems[i]->m_text)
            break;
    }
    m_childItems.insert(i, item);
}

102
bool ConsoleItem::insertChild(int position, ConsoleItem *item)
103 104 105 106 107 108 109 110 111
{
    if (position < 0 || position > m_childItems.size())
        return false;

    m_childItems.insert(position, item);

    return true;
}

112
ConsoleItem *ConsoleItem::parent()
113 114 115 116
{
    return m_parentItem;
}

117
bool ConsoleItem::removeChildren(int position, int count)
118 119 120 121 122 123 124 125 126 127
{
    if (position < 0 || position + count > m_childItems.size())
        return false;

    for (int row = 0; row < count; ++row)
        delete m_childItems.takeAt(position);

    return true;
}

128
bool ConsoleItem::detachChild(int position)
129 130 131 132 133 134 135 136 137
{
    if (position < 0 || position > m_childItems.size())
        return false;

    m_childItems.removeAt(position);

    return true;
}

138
void ConsoleItem::setText(const QString &text)
139 140 141 142 143 144 145 146
{
    m_text = text;
    for (int i = 0; i < m_text.length(); ++i) {
        if (m_text.at(i).isPunct())
            m_text.insert(++i, QChar(0x200b)); // ZERO WIDTH SPACE
    }
}

147
QString ConsoleItem::text() const
148 149 150 151
{
    return m_text;
}

152 153 154 155 156 157
QString ConsoleItem::expression() const
{
    QString text = m_text;
    return text.remove(QChar(0x200b));  // ZERO WIDTH SPACE
}

158
} // QmlJS