File: HelpDisplayWidget.cpp

package info (click to toggle)
clustalx 2.1%2Blgpl-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,868 kB
  • sloc: cpp: 40,050; sh: 163; xml: 102; makefile: 15
file content (162 lines) | stat: -rw-r--r-- 4,825 bytes parent folder | download | duplicates (5)
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
/**
 * Changes: 
 * 12-4-07, Mark Larkin, Removed destructor. No need to delete QObjects. Also removed
 * setAllPtrsToNull function.  
 *
 * 24-05-07,Nigel Brown(EMBL): simplified file searching mechanism; removed
 * filePath, executablePath members; made helpFileName static; removed
 * executablePath argument to constructor. Added Resources class to handle
 * file search.
 *
 * 06-06-07,Nigel Brown(EMBL): loadHelpInformation() now returns 'string *'
 * instead of 'void'; constructor tests this value to see if a help string was
 * successfully loaded then typesets it as HTML and scrolls to the top.
 */
#include "HelpDisplayWidget.h"
#include <QTextEdit>
#include <iostream>
#include <fstream>
#include <QtGui>
#include "Resources.h"

const string HelpDisplayWidget::helpFileName = "/usr/share/clustalx/clustalx.hlp";

HelpDisplayWidget::HelpDisplayWidget(char helpPointer)
{
    setWindowTitle("Help");
    
    helpText = new QTextEdit;
    helpText->setReadOnly(true);
    helpText->setMinimumWidth(500);
    helpText->setMinimumHeight(400);
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    mainLayout = new QVBoxLayout;
    
    okButton = new QPushButton(tr("Ok"));
    okButton->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Minimum);
    connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));    

    mainLayout->addWidget(helpText);
    mainLayout->addWidget(okButton);
    setLayout(mainLayout);

    /* nige */
    string *buff = loadHelpInformation(helpPointer);

    if (buff) {
        //cout << buff->c_str() << endl;
        helpText->setHtml(QString(buff->c_str()));

        //nige: scroll to top of text
        QTextCursor tc = helpText->textCursor();
        tc.movePosition(QTextCursor::Start);
        helpText->setTextCursor(tc);
        delete buff;
    }
}

string *HelpDisplayWidget::loadHelpInformation(char helpPointer)
{
    ifstream helpFile;
    int  i, number, nlines;
    bool foundHelp;
    char templine[MAXLINE + 1]; // To be used with get!
    char token = '\0';
    string digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    string helpMarker    = ">>HELP";
    
    Resources *res = Resources::Instance();
    string file = res->searchPathsForFile(helpFileName);
    //cout << "helpfile: " << file.c_str() << endl;

    //nige
    string *buff = new string("<BODY>");

    try
    {
        helpFile.open(file.c_str(), ifstream::in);

        if(!helpFile.is_open())
        {
            clustalw::utilityObject->error("Cannot open File file [%s]\n",
                                           helpFileName.c_str());
            return 0;
        }

        nlines = 0;
        number = -1;
        foundHelp = false;
        string tempS;

        while(!helpFile.eof())
        {
            if(!helpFile.getline(templine, MAXLINE + 1))
            {
                if(!foundHelp)
                {
                    clustalw::utilityObject->error("No help found in help file\n");
                }
                helpFile.close();
                return 0;
            }
            tempS = string(templine); 
            
            if(tempS.find(helpMarker, 0) != string::npos) // If found helpMarker! 
            {
                token = ' ';
                for(i = helpMarker.length(); i < 8; i++) // MAGIC 8??????
                {
                    if(digits.find(tempS[i], 0)  != string::npos)
                    {
                        token = tempS[i];
                        break;
                    }
                }
            }
            if(token == helpPointer) 
            {
                foundHelp = true;
                while(helpFile.getline(templine, MAXLINE + 1)) 
                {
                    tempS = string(templine);

                    if(tempS.find(helpMarker, 0) != string::npos) // If found another help
                    {
                        //nige: section terminated by next section anchor
                        helpFile.close();
                        *buff += "</BODY>\n";
                        return buff;
                    }
                    
                    //replace trailing newline on non-html
                    if (tempS[0] != '<') {
                        tempS += '\n';
                    }

                    //cout << tempS.c_str() << endl;
                    *buff += tempS;
                    ++nlines;
                }
                //nige: last section terminated by EOF
                helpFile.close();
                *buff += "</BODY>\n";
                return buff;
            }
        }
    }
    catch(ifstream::failure e)
    {
        return 0;
    }
    if(helpFile.is_open())
    {
        helpFile.close();
    }
    return 0;
}

void HelpDisplayWidget::accept()
{
    setResult(QDialog::Accepted);
    hide();
}