CommonTestsActions.cpp 7.08 KB
Newer Older
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
183
184
185
186
187
188
189
190
191
/*****************************************************************************
 * $CAMITK_LICENCE_BEGIN$
 *
 * CamiTK - Computer Assisted Medical Intervention ToolKit
 * (c) 2001-2013 UJF-Grenoble 1, CNRS, TIMC-IMAG UMR 5525 (GMCAO)
 *
 * Visit http://camitk.imag.fr for more information
 *
 * This file is part of CamiTK.
 *
 * CamiTK is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * CamiTK 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 Lesser General Public License version 3 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with CamiTK.  If not, see <http://www.gnu.org/licenses/>.
 *
 * $CAMITK_LICENCE_END$
 ****************************************************************************/

// -- Core stuff
#include <Application.h>
#include <MainWindow.h>
#include <InteractiveViewer.h>
#include <Core.h>
#include <ExtensionManager.h>
#include <Action.h>


using namespace camitk;

// Complete description
// If you modify this description, PLEASE do not forget to
// also update the associated "camitk-imp-prologue.1.in" man page
// (both texts should only be marginally different)
static const char* description = "This is a test application, it is "
                          "not intended to be used as a CamiTK application."
                          "\n -a <file> action to load (.dll file)"
                          "\n -i <file> is the file to open - will not be modified";

// usage for this application
static void usage(char *appName) {
    std::cout << appName << std::endl;
    std::cout << std::endl;
    std::cout << "Usage: " << appName << " -i <fileIn> -a <OutDirectory>" << std::endl;
    std::cout << "Built using " << Core::version << std::endl;
    std::cout << std::endl;
    std::cout << description << std::endl;
    std::cout << std::endl;
    std::cout << "Options:" << endl;

    std::cout << "Arguments:" << std::endl;
    std::cout << "-a <file> action name -i <file> input file on which the tested action will process" << std::endl;
}

int main(int argc, char *argv[]) {
    try {
        int ret = EXIT_SUCCESS;
        bool inputTest  = false;        
        bool actionTest = false;
        QFileInfo iaction;
        QFileInfo ifile;        
        
        std::cout << "Start " << argv[0] << std::endl;
        for (int i=1; i<argc; i++) 
        {
            if (!strcmp( argv[i],"-a" )) {
                iaction.setFile( argv[i+1] );
            
                actionTest = true;
            }        
            if (!strcmp( argv[i],"-i" )) {
                ifile.setFile( argv[i+1] );
                inputTest = true;
            }
        }

        // if no or not correct options provided
        if ( !inputTest || !actionTest ) 
        {
            if (argc>1) 
            {
                std::cout << "Argument errors: ";

                for(int i=1; i<argc; i++) 
                {
                    std::cout << "[" << argv[i] << "] ";
                }
            }
            usage(argv[0]);
            ret = EXIT_SUCCESS;
            return ret;
        }
        else
        {
            std::cout << "Arguments OK: ";
            bool extOpen = false;
            // init the camitk application context
            Application a( "action-test", argc, argv, false, false );// No autoload + No association of types extensions to CamiTK 
            a.getMainWindow()->setCentralViewer( InteractiveViewer::getAxialViewer() );
            
            ExtensionManager::autoload( ExtensionManager::COMPONENT );

            Component* comp = Application::open( ifile.absoluteFilePath() );                

            if( comp )
            {
                QString ActionDir;
                Action::ApplyStatus status;
                QStringList extensionDir;

                std::cout << "Open file OK : ";
                
                comp->setSelected( true, true );

                extensionDir = Core::getActionDirectories();

                foreach( ActionDir, extensionDir )
                {                    
                    if ( ExtensionManager::loadExtension( ExtensionManager::ACTION, ActionDir + '/' + iaction.fileName() ))
                    {                        
                        std::cout << "load action OK : ";
                        extOpen = true;
                    }
                }
                if( extOpen )
                {                        
                    QStringList componentHierarchy = comp->getHierarchy();
                            
                    ActionList actl = Application::getActions( comp );

                    foreach( Action *action , actl )
                    {        
                        QString q = action->getName();
                        std::cout << "action->getName() " <<  action->getName().data();                            
                            
                        if( componentHierarchy.contains( action->getComponent() ) )
                        {
                            std::cout << "componentHierarchy OK : ";
                            action->setInputComponent( comp );                    
                        
                             status = action->apply();
                                                                
                            if( status != Action::SUCCESS )
                            {
                                std::cout << "action->getname() " <<  action->getName().data() << "FAILED";
                                ret = EXIT_FAILURE;
                                break;
                            }
                        }
                    }           
                }
                else
                {
                    std::cout << "Loading action extension :" <<  iaction.fileName().data() << "FAILED";
                }
            }                
            else
            {
                std::cout << "Loading file :" <<  ifile.absoluteFilePath().data() << "FAILED";
            }
        
    return ret;
    
    }


               
    }
    catch (camitk::AbortException& e) {
        //QMessageBox::warning(NULL, QString(argv[0]) + " aborted...", "AbortException:" + QString(e.what()));
        std::cout << argv[0] << " aborted..." << std::endl << "camitk AbortException:" << std::endl << e.what() << std::endl;
        return EXIT_FAILURE;
    }
    catch (std::exception& e) {
        //QMessageBox::warning(NULL, QString(argv[0]) + " aborted...", "AbortException:" + QString(e.what()));
        std::cout << argv[0] << " aborted..." << std::endl << "std AbortException:" << std::endl << e.what() << std::endl;
        return EXIT_FAILURE;
    }
    catch (...) {
        //QMessageBox::warning(NULL, QString(argv[0]) + " aborted...", "Unknown Exception");
        std::cout << argv[0] << " aborted..." << std::endl << "Unknown Exception" << std::endl;
        return EXIT_FAILURE;
    }

}