summaryrefslogtreecommitdiffstats
path: root/buildtools/lib/parsers/qmake/tests/viewer.cpp
blob: dba2b7fb5e36f9e0c9c6a8d40f48a62bdff68684 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/***************************************************************************
 *   Copyright (C) 2005 by Alexander Dymo                                  *
 *   [email protected]                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "viewer.h"

#include <qdir.h>
#include <qlineedit.h>
#include <qlistbox.h>
#include <qfiledialog.h>
#include <qtextedit.h>
#include <qfile.h>
#include <qtextstream.h>
#include <qlistview.h>
#include <qtabwidget.h>

#include <qmakeast.h>
#include <qmakedriver.h>
#include <qmakeastvisitor.h>

using namespace QMake;

Viewer::Viewer(QWidget *parent, const char *name)
    :ViewerBase(parent, name), projectAST(0)
{
    if (QFile::exists(QDir::currentDirPath() + "/" + "qtlist"))
    {
        QFile f(QDir::currentDirPath() + "/" + "qtlist");
        f.open(IO_ReadOnly);
        QTextStream str(&f);
        while (!str.eof())
            files->insertItem(str.readLine());
    }
    ast->setSorting(-1);
//    parentProject.push((QListViewItem*)0);
}

void Viewer::addAll_clicked()
{
    if (allLocation->text().isEmpty())
        return;
    QDir d(allLocation->text());
    QStringList l = d.entryList("*.pro *.pri");
    for (QStringList::iterator it = l.begin(); it != l.end(); ++it)
        (*it) = QDir::cleanDirPath(allLocation->text() + "/" + (*it));
    files->insertStringList(l);
}

void Viewer::choose_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(QDir::currentDirPath(), "*.pro *.pri", this);
    if (!fileName.isEmpty())
        files->insertItem(fileName);
}

void Viewer::files_currentChanged(QListBoxItem* item)
{
    ast->clear();

    QFile f(item->text());
    f.open(IO_ReadOnly);
    QTextStream str(&f);
    source->setText(str.read());
    f.close();

    int result = QMake::Driver::parseFile(item->text().ascii(), &projectAST, 0);
    if (projectAST && (result == 0))
    {
        processAST(projectAST);
    }
    if (tabWidget2->currentPageIndex() == 1)
        tabWidget2_selected("Source to be written back");
}

void Viewer::tabWidget2_selected(const QString& text)
{
    if ((text == "Source to Be Written Back") && projectAST)
    {
        QString buffer;
        projectAST->writeBack(buffer);
        writeBack->setText(buffer);
    }
}

class ViewerVisitor: public ASTVisitor {
public:
    ViewerVisitor(Viewer *v): ASTVisitor()
    {
        this->v = v;
        parentProject.push((QListViewItem*)0);
    }

    virtual void processProject(ProjectAST *project)
    {
        ASTVisitor::processProject(project);
    }

    virtual void enterRealProject(ProjectAST *project)
    {
        QListViewItem *projectIt;
        if (!parentProject.top())
        {
            projectIt = new QListViewItem(v->ast, "Project");
            projectIt->setOpen(true);
            parentProject.push(projectIt);
        }

        ASTVisitor::enterRealProject(project);
    }
    virtual void enterScope(ProjectAST *scope)
    {
        QListViewItem *projectIt = new QListViewItem(parentProject.top(), scope->scopedID, "scope");
        parentProject.push(projectIt);
        ASTVisitor::enterScope(scope);
    }
    virtual void leaveScope(ProjectAST *scope)
    {
        parentProject.pop();
    }
    virtual void enterFunctionScope(ProjectAST *fscope)
    {
        QListViewItem *projectIt = new QListViewItem(parentProject.top(),
            fscope->scopedID + "(" + fscope->args + ")", "function scope");
        parentProject.push(projectIt);
        ASTVisitor::enterFunctionScope(fscope);
    }
    virtual void leaveFunctionScope(ProjectAST *fscope)
    {
        parentProject.pop();
    }
    virtual void processAssignment(AssignmentAST *assignment)
    {
        QListViewItem *item = new QListViewItem(parentProject.top(),
                assignment->scopedID, assignment->op, assignment->values.join("|"),
                "assignment");
        item->setMultiLinesEnabled(true);

        ASTVisitor::processAssignment(assignment);
    }
    virtual void processNewLine(NewLineAST *newline)
    {
        new QListViewItem(parentProject.top(), "<newline>");
        ASTVisitor::processNewLine(newline);
    }
    virtual void processComment(CommentAST *comment)
    {
        new QListViewItem(parentProject.top(), "<comment>");
        ASTVisitor::processComment(comment);
    }
    virtual void processInclude(IncludeAST *include)
    {
        new QListViewItem(parentProject.top(), "<include>", include->projectName);
        QMake::ASTVisitor::processInclude(include);
    }

    Viewer *v;
    QValueStack<QListViewItem *> parentProject;
};


void Viewer::processAST(QMake::ProjectAST *projectAST, QListViewItem *globAfter)
{
    ViewerVisitor visitor(this);
    visitor.processProject(projectAST);
}

#include "viewer.moc"